Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created September 28, 2011 16:52
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 barneycarroll/1248467 to your computer and use it in GitHub Desktop.
Save barneycarroll/1248467 to your computer and use it in GitHub Desktop.
Cross-browser method for injecting passed CSS into the document
function writeStyle(rules){
var head = document.getElementsByTagName('head')[0],
el = this.el || document.createElement('style'),
rules = rules || '';
if(!this.el)
el.type = 'text/css';
if(el.styleSheet){
el.styleSheet.cssText = rules;
}
else{
$(el).empty()
el.appendChild(document.createTextNode(rules))
};
if(!this.el)
head.appendChild(el);
this.el = el;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment