Created
April 25, 2012 10:02
-
-
Save sirbrad/2488644 to your computer and use it in GitHub Desktop.
Dynamically loading an external script with call back
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
var loadScript = function(url, callback) { | |
var d = document, | |
s = d.getElementsByTagName('script')[0], | |
done = false, | |
script = d.createElement("script"); | |
script.type = "text/javascript", | |
script.src = url; | |
script.onload = script.onreadystatechange = function() { | |
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { | |
done = true; | |
script.onload = script.onreadystatechange = null; | |
if (callback) { | |
callback(); | |
} | |
} | |
}; | |
s.parentNode.insertBefore(script, s); // Find the <script> tag and insert new script above it | |
}; | |
loadScript('URL', function(){ | |
//Call back code | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment