...

View Full Version : IE6 and document.getElementsByTagName not working at window.onload



rh2600
01-11-2006, 04:18 AM
When I call a js file from within the <head>, IE borks at var headRef section of the following:



window.onload = function () {

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

var newStyle = document.createTextNode('something);
styles.appendChild(newStyle);

var headRef = document.getElementsByTagName('head').item(0);
headRef.appendChild(styles);
}


document.getElementsByTagName('head').item(0) is returned as "null or undefined" so I guess that this is all happening before IE has built the DOM tree? Does window.onload not fire when the entire document is completed? I've tried document.onload too, no dice.

glenngv
01-11-2006, 06:20 AM
var headRef = document.getElementsByTagName('head')[0];

liorean
01-11-2006, 04:51 PM
This would seem to be the problem:


window.onload = function () {

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

var newStyle = document.createTextNode('something);
styles.appendChild(newStyle);

var headRef = document.getElementsByTagName('head').item(0);
headRef.appendChild(styles);
}
Unless that's just a typo when you posted?

rh2600
01-12-2006, 05:16 AM
Yup sorry, that was just a typo. But Glenngv's modification fixed one issue, it has exposed the other.

IE doesn't like the fact that I am creating an element called style. If I rename the element to anything else then IE returns no errors, but with 'style' IE says 'unexpected call to method or property access'. I guess its getting confused between an element 'style' and the property 'style'?


document.createElement('style')

I guess I will just have to declare styles as properties of an object, and not try to create them as an element in the header.... sigh.....

KC-Luck
01-12-2006, 05:15 PM
why not just use the document.createStyleSheet method? :)
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createstylesheet.asp

rh2600
01-13-2006, 05:07 AM
Because it is very important that *everything* that is done, is done within one (.js) file ;)

glenngv
01-13-2006, 08:20 AM
Huh? What do you mean? document.createStyleSheet will not create a CSS file physically, it returns a stylesheet object where you can dynamically add rules. Here's a good tutorial (http://www.javascriptkit.com/dhtmltutors/externalcss2.shtml) for this.

KC-Luck
01-13-2006, 03:32 PM
*everything* that is done, is done within one (.js) file
and how would this *not* be within one js file?
you would access the created styleSheet object and apply your styles to it's cssText, for IE, that is, or use the addRules(), blah stuff as glenn is referring to.


document.createStyleSheet will not create a CSS file physically, it returns a stylesheet object where you can dynamically add rules.
yes, exactly, is that not what he wants to do? dynamically append styles to his document?
with ie, you can bulk write to this [object] via .cssText, sort of haxish, but sometimes must be done to get around it's downfalls.

rh2600
01-14-2006, 01:24 PM
Sorry yes :)

You are right, this would allow me to feed styles to IE. I would prefer using standards based approach, so instead of using this method along with the createElement method, I am thinking about setting style attributes one-by-one.

I see what people mean when they moan about IE's DOM bugs though... ;)

Thanks for your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum