Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return value from js file

I have two js file in my html page . If the first one begins with :

 (function($){
   ..  
   ..
   }(jQuery));

can I insert a var into function($,varname) , return it's value and use it in the other file?

like image 719
steo Avatar asked Nov 20 '25 11:11

steo


1 Answers

You need a global variable for this. You can do this in one of a few ways. Let's assume we need to send the value "Bacon" to the other script.

(function($){
   window.myScriptsExports = "Bacon";
}(jQuery));

// OR

var myScriptsExports = (function($){
   // other code
   return "Bacon";
   // NO other code
}(jQuery));

// OR (not really recommended)

(function($){
   // other code
   $.myScriptsExports = "Bacon";
   // other code
}(jQuery));
like image 106
Brigand Avatar answered Nov 23 '25 02:11

Brigand