Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denji/687f252ea14099bf22458309ca563cbb to your computer and use it in GitHub Desktop.
Save denji/687f252ea14099bf22458309ca563cbb 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