Skip to content

Instantly share code, notes, and snippets.

@yoannmoinet
Created January 11, 2013 14:35
Show Gist options
  • Save yoannmoinet/4511093 to your computer and use it in GitHub Desktop.
Save yoannmoinet/4511093 to your computer and use it in GitHub Desktop.
Add a file to the page.
function addScript(type,url,cache,cb){
var script = document.createElement(type),
head=document.getElementsByTagName('head')[0],
done=false,
src = url;
src += (cache === false)?('?dummy=' + (+new Date())):'';
script.onload=script.onreadystatechange = function(){
if ( !done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete') ) {
done=true;
if(typeof cb === 'function') cb();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
script.src = script.href = src;
head.appendChild(script);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment