Last active
February 13, 2016 03:18
-
-
Save MiguelCastillo/dd632093019da89d1b11 to your computer and use it in GitHub Desktop.
script loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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