Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
Created May 19, 2012 20:47
Show Gist options
  • Save LukeChannings/2732326 to your computer and use it in GitHub Desktop.
Save LukeChannings/2732326 to your computer and use it in GitHub Desktop.
stylesheet injector
var load = function(url, callback) {
if ( ! window.registeredStylesheets ) window.registeredStylesheets = [];
if ( window.registeredStylesheets.indexOf(url) == -1)
{
(document.head || document.getElementsByTagName("head")[0]).appendChild(function(){
var link = document.createElement('link');
link.setAttributes({
'rel' : 'stylesheet',
'type' : 'text/css',
'href' : url
});
return link;
}());
if ( typeof callback == 'function' ) {
(function checkLoaded() {
for ( var i = 0; i < document.styleSheets.length; i++ ) {
if ( new RegExp(url).test(document.styleSheets[i].href) ) {
callback()
return;
}
}
setTimeout(checkLoaded, 1)
})()
}
window.registeredStylesheets.push(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment