Skip to content

Instantly share code, notes, and snippets.

@MiguelCastillo
Last active February 13, 2016 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiguelCastillo/dd632093019da89d1b11 to your computer and use it in GitHub Desktop.
Save MiguelCastillo/dd632093019da89d1b11 to your computer and use it in GitHub Desktop.
script loader
function loadScript(url) {
var head = document.getElementsByTagName("head")[0] || document.documentElement;
var script = document.createElement("script");
script.setAttribute("async", "true");
script.setAttribute("charset", "utf-8");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", url);
//
// Code from:
// http://stackoverflow.com/questions/4845762/onload-handler-for-script-tag-in-internet-explorer
// http://stevesouders.com/efws/script-onload.php
//
return new Promise(function(resolve) {
// Handle Script loading
var done = false;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState ||
this.readyState === "loaded" ||
this.readyState === "complete")) {
done = true;
// Get the module id that just finished and load it up!
// var loadedSrc = script.getAttribute("src");
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if (head && script.parentNode) {
head.removeChild(script);
}
resolve();
}
};
head.appendChild(script);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment