Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element.appendChild() chokes in IE

I have the following javascript:

  css = document.createElement('style');
  css.setAttribute('type', 'text/css');
  css_data = document.createTextNode('');
  css.appendChild(css_data);
  document.getElementsByTagName("head")[0].appendChild(css);

for some reason, in IE only, it chokes on "css.appendChild(css_data);" Giving the error: "Unexpected call to method or property access"

What's going on?

like image 550
Adam Haile Avatar asked Dec 10 '22 22:12

Adam Haile


1 Answers

Try instead:

var css = document.createElement('style');
css.setAttribute('type', 'text/css');

var cssText = '';
if(css.styleSheet) { // IE does it this way
    css.styleSheet.cssText = cssText
} else { // everyone else does it this way
    css.appendChild(document.createTextNode(cssText));
}

document.getElementsByTagName("head")[0].appendChild(css);
like image 125
Crescent Fresh Avatar answered Dec 12 '22 11:12

Crescent Fresh