javascript - jQuery scope with noconflict in wordpress -


i'm bringing template across old version of joomla wordpress , got quite few javascript files have use main referring out functions in others. of these files use jquery left right , centre , given wordpress uses noconflict default thought straightforward wrap each js file this:

(function($){ ....my code... })(jquery); 

the problem scope of functions called across files, example:

file 1

(function($){     $(document).ready(function(){         mainmenu();     }); })(jquery); 

file 2

(function($){     function mainmenu(){         alert("hello");     } })(jquery); 

here problem error "uncaught referenceerror: mainmenu not defined", know simple reference function, can't see it, "doh" moment. appreciated. yes know find , replace '$' 'jquery' thought there must way it?

it because mainmenu closure function within anonymous function in second file.. not available in file1.

one possible solution make global function, available in global scope.

(function($){     window.mainmenu = function (){         alert("hello");     } })(jquery); 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -