Skip to content

Instantly share code, notes, and snippets.

@z-------------
Created June 23, 2014 13:24
Show Gist options
  • Select an option

  • Save z-------------/46ad90bbc2db0e2b83f0 to your computer and use it in GitHub Desktop.

Select an option

Save z-------------/46ad90bbc2db0e2b83f0 to your computer and use it in GitHub Desktop.
Asynchronously load external JavaScript files
var loadScriptAsync = function(urls, callback) {
var loadedList = [];
for (i=0; i<urls.length; i++) {
var scriptElem = document.createElement("script");
scriptElem.src = urls[i];
scriptElem.onload = function(){
loadedList.push(true);
if (loadedList.length == urls.length) {
callback();
}
}
document.head.appendChild(scriptElem);
}
})
// urls - string array of script urls
// callback - code to be executed after all scripts are loaded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment