Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created March 15, 2014 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/9571121 to your computer and use it in GitHub Desktop.
Save james2doyle/9571121 to your computer and use it in GitHub Desktop.
loadScript. Async load a script and then fire a callback when loaded.
//this function will work cross-browser for loading scripts asynchronously
function loadScript(src, callback) {
var s,
r,
t;
r = false;
s = document.createElement('script');
s.type = 'text/javascript';
s.src = src;
s.onload = s.onreadystatechange = function() {
//console.log( this.readyState ); //uncomment this line to see which ready states are called.
if (!r && (!this.readyState || this.readyState == 'complete')) {
r = true;
callback();
}
};
t = document.getElementsByTagName('script')[0];
t.parent.insertBefore(s, t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment