| home / programming / javascript / detection | [previous] |
|
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:
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:
opera7 = window.opera &&
document.createComment;
oldopera = window.opera &&
!document.createComment;
Another detection method I find useful is to separate Mac IE5 from other browsers:
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:
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:
Ie4upnoopera =document.all &&
!window.opera;
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 could use the following routine:
ie55= document.all &&
document.fireEvent &&
!document.createComment;
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.
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/.
| home / programming / javascript / detection | [previous] |
Created: February 3, 2003
Revised: February 3, 2003
URL: http://webreference.com/programming/javascript/detection/3.html