Forum Moderators: open
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?
If something in the script absolutely had to work in all browsers, I'd put it on the server side and not take chances.
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.