views:

2610

answers:

5

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?

+6  A: 

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);
Crescent Fresh
A: 

This is particular with the "style" element, IE doesn't allow the appendChild() method on it.

Luca Matteis
A: 

no appendChild() allowed with HTMLStyleElement in I.E.

check it out

ForYourOwnGood
A: 

There is a pretty long discussion here on the bug:

http://www.quirksmode.org/bugreports/archives/2006/01/IE_wont_allow_documentcreateElementstyle.html

Eric

epascarello
A: 

@crescentfresh

I tried your suggestion, and the content of the style block simply never gets populated. Tried in IE6 and IE7... it just doesn't seem to do anything

Here's my modified code:

function load_content()
{
  var d = new Date();

  css = document.createElement('style');
  css.setAttribute('type', 'text/css');
  if(css.styleSheet) { css.styleSheet.cssText = 'testing'} //Because IE is evil
  else { css_data = document.createTextNode(''); css.appendChild(css_data); } //And everyone else is cool
  document.getElementsByTagName("head")[0].appendChild(css);

  new Ajax.PeriodicalUpdater('content', '/%doc_path%?'+d.getTime(),
  {
    method: 'post',
    frequency: 5,
    onSuccess: function(transport) {
          new Ajax.Request('/%css_path%?'+d.getTime(), {
              method: 'get',
              onSuccess: function(transport) {

                if(css.styleSheet) { css.styleSheet.cssText = transport.responseTex}
                else { 
                    var new_css_data = document.createTextNode(transport.responseText);
                    css.replaceChild(new_css_data, css_data); 
                    css_data = new_css_data;
                } 
              }
          });
    }
  });
}

Any ideas?

Adam Haile
Two things: 1) How are you verifying that the style block does not get the content? IE Developer toolbar or something? 2) Instead of 'testing' try 'body { font-size:45px; }' (iow try legitimate css).
Crescent Fresh
I have a bookmarklet that will show you the in memory source of the document, not what the source of the file is...not sure what you mean for #2
Adam Haile
css.styleSheet.cssText = 'body { font-size:45px; }';
Crescent Fresh
Does the bookmarklet show you css inside other <style> tags on the page?
Crescent Fresh
yes...the bookmarklet shows the css inside the style tags. it also shows it in that particular tag on other browsers.
Adam Haile
Thwo things: 1) What's the bookmarklet? 2) Did you try css.styleSheet.cssText = 'body { font-size:45px; }';? That should give you a visual indication if it's working.
Crescent Fresh
The bookmarklet is here: http://adamhaile.com/projects/bookmarkletsI tried, your other suggestion and it led me to my final solution, which actually was a problem with another section of code messing with this one. Thanks for all the help.
Adam Haile