Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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