PDA

View Full Version : if (document.getElementById) and IE on a MAC problem


valeria_vi
11-14-2002, 03:18 PM
here's a piece of my function:

if (document.layers) {
document.layers["point-of-purchase-display-kits"].document.kit.src = "popkits/0" + new_kit + ".gif";
}
else {
if (document.getElementById) {
alert("1")
document.getElementById('kit').src = "popkits/0" + new_kit + ".gif";
}
else {
alert("2")
document.kit.src = "popkits/0" + new_kit + ".gif";
}
}


new_kit is a variable containing a number.
i have a layer on a page and that layer has an image with a name "kit" in it.

works great in NS on PC and MAC, as well as IE on a PC.
IE 5.0 on a MAC alerts "1" (which means it understands "if (document.getElementById)"), but then does not change an image and gives me an error saying "document.getElementById(...) is not an object".

document.kit.src = "popkits/0" + new_kit + ".gif"; works (again, ie on a mac), but how can it recognise document.getElementById and then give me such an error? is my conditional statement wrong?

valeria_vi
11-14-2002, 03:25 PM
ok, just in case, i decided to post the whole function here (it isn;t that long)

var current = 1;

function kits_nav(val) { //val can be "1" or "-1"

var new_kit = current + val;

if (new_kit < 1) {
alert("You can't go any further back!");
new_kit = new_kit + 1
}

if (new_kit > 8) {
alert("You can't go any further forward!");
new_kit = new_kit - 1
}

if (document.layers) {
document.layers["point-of-purchase-display-kits"].document.kit.src = "popkits/0" + new_kit + ".gif";
}
else {
if (document.getElementById) {
alert("1")
document.getElementById('kit').src = "popkits/0" + new_kit + ".gif";
}
else {
alert("2")
document.kit.src = "popkits/0" + new_kit + ".gif";
}
}

current = new_kit;
}

brothercake
11-14-2002, 04:24 PM
Think about what you said:


Originally posted by valeria_vi
i have a layer on a page and that layer has an image with a name "kit" in it.


The expression

document.getElementById('kit')

finds an object with the id of "kit" - not the name. However if you change the name to an id, it break your netscape 4 script. So you could either give that image an identical name and id attribute, or you could just use the images collection:

document.images["kit"].src = ...

joh6nn
11-14-2002, 04:39 PM
i suggest using the images collection. in my experience, giving things identical names and ids, breaks in IE. it seems to have a particularly hard time with identically named and id'ed form elements.

valeria_vi
11-14-2002, 04:40 PM
eeek,
isn't it one of those "ID ten T" errors (ID10T)? :o

thanks so much!