Skip to content

Instantly share code, notes, and snippets.

@simondahla
Last active August 26, 2018 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simondahla/7d6698a73b689ad9bf1d9f2b63abdd62 to your computer and use it in GitHub Desktop.
Save simondahla/7d6698a73b689ad9bf1d9f2b63abdd62 to your computer and use it in GitHub Desktop.
/**
* 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