Created
December 1, 2012 05:02
-
-
Save digitalicarus/4180610 to your computer and use it in GitHub Desktop.
load js and css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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