Created
August 3, 2011 19:46
-
-
Save benjamn/1123597 to your computer and use it in GitHub Desktop.
load dynamic script with 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 fetch(src, callback) { | |
var script = document.createElement(nodeName), | |
called; | |
try { | |
script.addEventListener("load", callback, false); | |
} catch (x) { | |
script.attachEvent("onreadystatechange", function() { | |
if (!called && /loaded|complete/.test(script.readyState) { | |
called = true; | |
callback(); | |
} | |
}); | |
} | |
script.src = src; | |
document.documentElement.firstChild.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment