Skip to content

Instantly share code, notes, and snippets.

@jochemstoel
Created October 31, 2018 00:04
Show Gist options
  • Select an option

  • Save jochemstoel/efbadd0bd4756386616fc9ae0b5c924e to your computer and use it in GitHub Desktop.

Select an option

Save jochemstoel/efbadd0bd4756386616fc9ae0b5c924e to your computer and use it in GitHub Desktop.
JavaScript: loadScripts
// Loads js files
function loadScripts(urlArray,cb) {
var scriptLoadCount = scriptLoadedCount = 0;
loadScript();
function loadScript() {
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
var url = (urlArray[scriptLoadCount].indexOf('?') == -1) ? urlArray[scriptLoadCount] + '?d=' + Math.random() : urlArray[scriptLoadCount] + '&d=' + Math.random()
script.src= url;
head.appendChild(script);
scriptLoadCount++;
script.onload = function() {
scriptLoadedCount++;
if (scriptLoadedCount < urlArray.length) loadScript();
else if (cb) cb();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment