Skip to content Skip to sidebar Skip to footer

Using Javascript To Load Other External Javascripts

I have a library of JS code to load from a folder. Instead of typing the lines one by one in the tag of the HTML document, is there a way o

Solution 1:

This is the code you need:

    // Create
    var bodyEl = document.body;
    var scriptEl = document.createElement('script');
    scriptEl.type = 'text/javascript';
    scriptEl.src = url;
    bodyEl.appendChild(scriptEl);

Put that into a function, have an array of all the javascript files, and call that function for each file.

Benefits of using the DOM is that document.write doesn't work in some funny instances. More about this here: document.write() vs inserting DOM nodes: preserve form information?

Code taken from the open source project jQuery Sparkle: http://github.com/balupton/jquery-sparkle/blob/master/scripts/resources/jquery.appendscriptstyle.js#L103


Solution 2:

Try use requireJs, he have very userful functions

Official website


Post a Comment for "Using Javascript To Load Other External Javascripts"