isIE=document.all; //only kind of true

Internet Explorer 4 used document.all['elementName'] to refer to document elements while waiting for the W3C standard, which turned out to be document.getElementById('elementName'). IE 5 and above, plus all other modern standards based browsers, all support document.getElementByID(). IE 4 usage was below .5% in 2002 and isn't even being tracked anymore, so there's no good reason to be using document.all anymore.

Firefox developers decided to partially support this non-standard syntax since so many pages are still using it. It was probably a good decision - if they didn't, people would decide that Firefox was no good since it couldn't run their favorite pages, even though real problem was buggy webpages.

On the other hand, they didn't want to encourage use of non-standards compliant code, and they certainly didn't want to break all those poorly written scripts that (incorrectly) assumed the following:
isIE=document.all;

So Firefox supports document.all, but says it doesn't. That means it can evaluate document.all['elementName'], but alert(document.all) shows false.

This behavior makes some sense, but can result in some weird behavior. Scripts that were written for IE only (using document.all[] syntax) will work in Firefox, but scripts which test for IE with document.all but don't include proper handling for non-IE browsers don't work.

There are some much better solutions to the document.all mess:

1. Obviously, the best is to convert from document.all[] to document.getElementById(). It's standards compliant, works in 99+% of browsers, has a great beat, and is easy to dance to. Usually it's a simple find / replace exercise. However, it's sometimes not practical when you've got legacy scripts or browsers to deal with.

2. If you have to support IE 4 David Emmerson came up with a great work-around: add document.getElementById support to IE4 with this little script:

if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

3. If you absolutely have to write browser dependant code, use a decent browser detection script. This one is pretty solid: http://webreference.com/tools/browser/javascript.html

The best general approach is to check for support of a feature rather than to sniff IE and apply IE / non-IE logic. If you need to determine if you're looking at IE, you can just ask nicely (does navigator.appName = 'MSIE'?).

Of course, cross browser Javascript is a complex subject. Here are some good references:
http://www.stopbadtherapy.com/standards.shtml
http://www-128.ibm.com/developerworks/web/library/wa-ie2mozgd/
powered by IMHO 1.2

No Comments