Skip to content

Instantly share code, notes, and snippets.

@samperrow
Last active March 16, 2019 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samperrow/811ab1ff9548b38583c886e7dbbed53e to your computer and use it in GitHub Desktop.
Save samperrow/811ab1ff9548b38583c886e7dbbed53e to your computer and use it in GitHub Desktop.
This script will load multiple JS files dynamically without jQuery
var loadJS = function() {
var index = 0;
function createScript(url, location) {
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.location = location;
script.onload = indexCount;
return script;
}
var scripts = [
createScript('/path/scriptOne.js', document.head),
createScript('/path/scriptTwo.js', document.head),
createScript('/path/scriptThree.js', document.body),
createScript('/path/scriptFour.js', document.body)
];
function sendScript(index) {
scripts[index].location.appendChild(scripts[index]);
}
function indexCount() {
if (index < scripts.length) {
sendScript(index);
index++;
}
}
indexCount();
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment