Skip to content

Instantly share code, notes, and snippets.

@werty1st
Created September 21, 2015 14:27
Show Gist options
  • Save werty1st/d3ce35c96c6950014dc6 to your computer and use it in GitHub Desktop.
Save werty1st/d3ce35c96c6950014dc6 to your computer and use it in GitHub Desktop.
function loadScript(url,callback,args) {
scriptEl = document.createElement('script');
scriptEl.type = 'text/javascript';
scriptEl.async = true;
scriptEl.src = url;
if (typeof(callback) === 'function'){
if ("onload" in scriptEl){
scriptEl.onload = function(){
// remote script has loaded
if (args){
callback.apply(null,args);
} else {
callback.apply();
}
};
} else {
var r = false;
scriptEl.onreadystatechange = function() {
if (!r && (!this.readyState || this.readyState === 'complete')) {
r = true;
if (args){
callback.apply(null,args);
} else {
callback.apply();
}
}
};
}
}
document.getElementsByTagName('head')[0].appendChild(scriptEl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment