Browser determination

Many javascript snippets I've seen test to see what kind of browser they are running in using the following methods:

the boolean value of (document.getElementById) is used to indicate whether or not the browser is DOM compatible

the boolean value of (document.all) is used to determine if the browser is IE4

the boolean value of (document.layers) is used to determined if the browser is Netscape 4.

I'd like a brief explanation of why these properties indicate the corresponding browsers.  Thanks.
Who is Participating?
Because document.layers is not available in IE4 or other browsers and document.all is not available in Netscape.

document.getElementById is currently the most used function to retrieve specific elements in a html tree. In older browsers this function does not exists and therefor needs to be testen on, when you have people with older browser versions in your target public.

Michel PlungjanIT ExpertCommented:
However document.all is supposedly available in Opera.
You shouldn't be testing for browser.  Instead, you should be testing for object/method support prior to using something that may or may not be supported by the user's browser.
to detect you could check for
which is not supported by any other browser

and to correctly check for IE with document.all you need to do
if (document.all && !window.opera)

like this you exclude opera with document.all
For so many reasons, browser detection is the *wrong* way to go.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.