Created
March 9, 2012 00:57
-
-
Save mrclay/2004404 to your computer and use it in GitHub Desktop.
Load JS scripts asynchronously w/ callback
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(src, callback) { | |
var s = document.createElement("script"), | |
onEvent = ('onreadystatechange' in s) ? 'onreadystatechange' : 'onload'; | |
s[onEvent] = function () { | |
if (("loaded,complete").indexOf(this.readyState || "loaded") > -1) { | |
s[onEvent] = null; | |
if (callback) { | |
callback(); | |
} | |
s.parentNode.removeChild(s); | |
} | |
}; | |
s.src = src; | |
document.body.appendChild(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment