Can I call one js file from another.

I have an asp.net mvc application and I'm trying to move all my javascript to js files. right now I have one called sharedfunctions.js, which is used everywhere.

the problem is that if I translate some code into the js file, the calling method in the sharedfunctions.js file, it no longer works.

What is the best way to account for common code among js files and let them work fine?

+5
source share
5 answers

As long as all .js files are requested on the page, functions / variables in all of them will be available to any other users after loading the page.

, . , .js .

javascript, (, ), , sharedfunctions.js . sharedfunctions.js javascript.

, , . , , sharedfunctions.js script .

IMHO, , js js .

+5

, , ? , , , , .

if (typeof yourFunctionName == 'function')

+1

, , , - , .

JS ( moo-tools, )

$$('head')[0].adopt(new Element('script',{'type':'text/javascript','src':'path to js file'}));
0

Dojo: dojo.require("my.package.name");

jQuery: $.getScript("url/to/script.js", function() { /* callback */ });

0

:

function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>'); 
}
-1

All Articles