Forum Moderators: open

Message Too Old, No Replies

Browser compatibility for getElementById

         

rocknbil

6:14 pm on Dec 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have seen a lot of usage of

document.getElementById

on this board and I wonder about compatibility issues.

I came across an article on Devedge that addressed the browser identification issue. The bottom line was that B.I.D. can be a maintenance nightmare, a better solution is to test for the objects you want to manipulate and act accordingly, it doesn't matter what browser it is.

They proposed a simple solution that has worked great for me:

function whatever() {
if ((document.getElementById) ¦¦ (document.all) ¦¦ (document.layers)) {
if (document.getElementById) {
//set vars, do stuff for compliant browsers
}
else if (document.layers) {
//do stuff for NN4, basically
}
else if (document.all) {
//old IE compatibility
}
}
}

Isn't this form of compatibility testing necessary, or are the samples with only getElementByID ignoring any attempt at old browser support, either by example or intent?

garann

6:49 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



Personally, I almost always use getElementById and nothing else. I figure that whatever I'm using JavaScript for is optional by definition (since the user can turn it off altogether), so there's no need to struggle to support old browsers and write my script three or more different ways.

If something in the script absolutely had to work in all browsers, I'd put it on the server side and not take chances.

BjarneDM

1:49 am on Dec 16, 2004 (gmt 0)

10+ Year Member



document.getElementById is supported in
- IE 5 and upwards
- all Gecko-based browsers
- Safari
- Konqueror
- Opera

The script you've posted is taking into account the old version 4 and earlier of both IE and Netscape. If you don't need support for these old browsers just go for document.getElementById et al and don't worry about that kind of convoluted code.

The (almost) only case where you really have to destinguish is if you are capturing events, as IE - as usual - has it's own ideas as to how that should be implemented and hasn't implemented the w3c standard.