Skip to content

Instantly share code, notes, and snippets.

@digitalicarus
Created December 1, 2012 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalicarus/4180610 to your computer and use it in GitHub Desktop.
Save digitalicarus/4180610 to your computer and use it in GitHub Desktop.
load js and css
function asyncLoader (s, cb) {
var d = document
, h = document.getElementsByTagName('head')[0]
, b = (Math.random()*1e8>>0)
, i = 'loader_'+b
, t = (/\.js$/.test(s)) ? "js" : "css"
, j = (t === 'js') ? d.createElement('script') : d.createElement('link');
j.id = i;
if(t === "js"){
j.src = s+'?'+'b='+b;
h.appendChild(j);
j.onload = j.onreadystatechange = function() {
var r = this.readyState;
if ((!r || r == "loaded" || r == "complete")) {
// Handle memory leak in IE
j.onload = j.onreadystatechange = null;
h.removeChild( j );
cb && typeof cb === 'function' && cb();
}
};
} else {
j.setAttribute("rel", "stylesheet");
j.setAttribute("type", "text/css");
j.setAttribute("href", s+'?'+'b='+b);
h.appendChild(j);
//-- need poller here
cb && typeof cb === 'function' && cb();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment