Skip to content

Instantly share code, notes, and snippets.

@hideki-a
Created May 2, 2012 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hideki-a/2574404 to your computer and use it in GitHub Desktop.
Save hideki-a/2574404 to your computer and use it in GitHub Desktop.
Dynamic style element
// See Also: http://www.phpied.com/dynamic-script-and-style-elements-in-ie/
// http://code.skyward-design.net/js/code_19.html
var head = document.getElementsByTagName("head")[0],
style = document.createElement("style"),
def = "body { color: red; }";
head.appendChild(style);
if (typeof window.attachEvent === "object" && style.styleSheet) {
// Less than IE9
style.styleSheet.cssText = def;
} else {
style.appendChild(document.createTextNode(def));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment