Skip to content

Instantly share code, notes, and snippets.

@sirbrad
Created April 25, 2012 10:02
Show Gist options
  • Save sirbrad/2488644 to your computer and use it in GitHub Desktop.
Save sirbrad/2488644 to your computer and use it in GitHub Desktop.
Dynamically loading an external script with call back
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