Load js files dynamically through another js file?

one way or another, load other JS files from one JS file. I would like to specify separate pages of the "ITS" js file and that the js file will load jquery, other js files.

I know that I can just put it all in html ie

I was thinking about sharing problems, so I was wondering if something already exists without me reinventing the wheel ...

I just would have to edit home.js to change what other js (jquery etc.) load for home.htm ... home.htm will just point to home.js

thanks

+7
javascript jquery
source share
5 answers

You can see the dynamic loading script . Here is an excerpt from the article:

var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'helper.js'; head.appendChild(script); 
+15
source share

For external communication JS

 var loadJs = function(jsPath) { var s = document.createElement('script'); s.setAttribute('type', 'text/javascript'); s.setAttribute('src', jsPath); document.getElementsByTagName('head')[0].appendChild(s); }; loadJs('http://other.com/other.js'); 

For the same JS domain link (using jQuery)

 var getScript = function(jsPath, callback) { $.ajax({ dataType:'script', async:false, cache:true, url:jsPath, success:function(response) { if (callback && typeof callback == 'function') callback(); } }); }; getScript('js/other.js', function() { functionFromOther(); }); 
+3
source share

This is similar to Darin's decision, except that he does not make any variables.

 document.getElementsByTagName('head')[0].appendChild(document.createElement("script")).src = "helper.js"; 
+2
source share

I would advise you to take a look at labJS. This is a library specifically designed to load Javascript. As the saying goes ... "The main goal of LABjs is to be a universal on-demand JavaScript loader, capable of downloading any JavaScript resource from anywhere to any page at any time."

See the labJS homepage for more information.

+2
source share

Google offers centrally hosted versions of major javascript libraries such as jQuery. They can be dynamically loaded using google loader.

http://code.google.com/apis/ajaxlibs/documentation/

+1
source share

All Articles