Forked from simondahla/insert-css-to-head-for-google-optimize.js
Created
August 26, 2018 03:17
-
-
Save denji/687f252ea14099bf22458309ca563cbb to your computer and use it in GitHub Desktop.
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 adds style element to the bottom of the head element of the page | |
* @param {string} code Any string of css code | |
* @return {undefined} | |
*/ | |
function insertCssToHead( code ) { | |
var style = document.createElement('style'); | |
style.type = 'text/css'; | |
if (style.styleSheet) { | |
// IE | |
style.styleSheet.cssText = code; | |
} else { | |
// Other browsers | |
style.innerHTML = code; | |
} | |
document.getElementsByTagName('head')[0].appendChild( style ); | |
} | |
// Example usage; | |
// insertCssToHead('body{ background-color: red; }'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment