Created
September 19, 2014 14:45
-
-
Save yuanyan/b9dbe51f676c47c17499 to your computer and use it in GitHub Desktop.
Injects the CSS into the <head> DOM node.
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
/** | |
* 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