JavaScript Object Detection (3/3) | WebReference

JavaScript Object Detection (3/3)

To page 1To page 2current page
[previous]

Object Detection in the New Browser Age

Netscape 6+

To separate Netscape 6+ from Internet Explorer we can use the following:

ns6 = !document.all && 
      document.getElementById;

Because we negate the document.all condition from the statement, only browsers that support document.getElementById but that are not Internet Explorer will be allowed to use the code. If we further wanted to separate out Netscape 6 from Opera we could do the following:

Netscape 6+ no Opera

ns6noopera = !document.all && 
             !window.opera && 
             document.getElementById;

While on Opera, I often find a need to separate Opera 7 from Opera 6, because of the now excellent standards support of Opera 7. The following will separate the two browsers:

Opera 7

opera7 = window.opera && 
         document.createComment;

Opera 6 and below

oldopera = window.opera && 
           !document.createComment;

Another detection method I find useful is to separate Mac IE5 from other browsers:

Internet Explorer 5 on Macintosh with IE4 and Opera

macie5 = document.all && 
         !document.mimeType;

The above will allow Internet Explorer 5 and above on Windows to ignore whatever code is used in the condition, but allows Internet Explorer 5 on Mac to utilize the code. If we did not want Internet Explorer 4 to use the code we could expand on the object detection routine so that it appears like so:

Internet Explorer 5 on Macintosh, but not Internet Explorer 4

macie5notothers = document.all && 
                  document.getElementById && 
                  !document.mimeType &&
                  !windows.opera;

Let us turn our attention to Internet Explorer on Windows. If we wanted to distinguish between Internet Explorer 4+ and Opera we could use this:

Internet Explorer 4 and above, but no Opera

Ie4upnoopera =document.all && 
              !window.opera;

Internet Explorer 5.0

If we wanted only Internet Explorer 5 then the following would do the trick:

ie5 = document.all && 
      !document.fireEvent && 
      !window.opera;

Internet Explorer 5.5

Internet Explorer 5.5 could use the following routine:

ie55= document.all && 
      document.fireEvent && 
      !document.createComment;

Internet Explorer 6

For Internet Explorer 6 the following would apply:

ie6 = document.all && 
      document.fireEvent && 
      document.createComment;

As you can see object detection works and works quite well, provided that you research which object is supported by the specific browser you want to code for. Luckily, the newer generations of browsers are now becoming more standards aware. Hopefully, there will come a time when we won't need to separate out browsers; but for now feel free to use the tactics articulated in this article to identify your browsers.

About the Author

Eddie Traversa is a multiple award winning developer who works as an independent Web consultant. He likes to play around with the latest technologies, and is currently writing a book on Dynamic XHTML with Jeff Rouyer of http://www.htmlguru.com/ fame. His experimental DHTML site is located at http://dhtmlnirvana.com/.


To page 1To page 2current page
[previous]

Created: February 3, 2003
Revised: February 3, 2003

URL: http://webreference.com/programming/javascript/detection/3.html