Skip to content

Instantly share code, notes, and snippets.

@ltchronus
Created February 7, 2015 06:18
Show Gist options
  • Save ltchronus/2f2c0f8469a3f42f1233 to your computer and use it in GitHub Desktop.
Save ltchronus/2f2c0f8469a3f42f1233 to your computer and use it in GitHub Desktop.
load scripts asynchronously
//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.parentNode.insertBefore(s, t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment