Skip to content

Instantly share code, notes, and snippets.

@yuanyan
Created September 19, 2014 14:45
Show Gist options
  • Save yuanyan/b9dbe51f676c47c17499 to your computer and use it in GitHub Desktop.
Save yuanyan/b9dbe51f676c47c17499 to your computer and use it in GitHub Desktop.
Injects the CSS into the <head> DOM node.
/**
* Injects the CSS into the <head> DOM node.
*
* @param {String} css CSS string to add to the <style> tag.
* @param {Document} doc document instance to use.
*/
function loadStyles(css, doc) {
// default to the global `document` object
if (!doc) doc = document;
var head = doc.head || doc.getElementsByTagName('head')[0];
if (!head) throw new Error('could not find <head> DOM node');
var style = doc.createElement('style');
style.type = 'text/css';
if (style.styleSheet) { // IE
style.styleSheet.cssText = css;
} else { // the world
style.appendChild(doc.createTextNode(css));
}
head.appendChild(style);
return style;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment