Try adding the style
element to the head
rather than the body
.
This was tested in IE (7-9), Firefox, Opera and Chrome:
var css="h1 { background: red; }",
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
head.appendChild(style);
style.type="text/css";
if (style.styleSheet){
// This is required for IE8 and below.
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}