Browser Sniffer Class
XML Menu Suite
Build powerful, client-side DHTML menus using XML for data and CSS for style. No JavaScript data! No having to place list elements on every single page of your website! No plug-ins!

Class Name: CJL_BrowserSniffer

Summary:

Lightweight JavaScript browser sniffer/detector class that gives information about browser type and version.

Submitted: 01/28/2003

Created by: CodeHouse.com

Browser Compatibility

  • Internet Explorer 5.0+/Mac
  • Internet Explorer 5.0+/Windows
  • Netscape 6.1+
  • Mozilla 1.0+
  • Opera 7.0+
  • Safari 1.0+

Demo

The demo opens in a new window. It displays what browser you're using, and what version it is.

To see the source code, use your browser's method of viewing the contents of the page, or save the page to your local machine and view it in an editor.

Source Code

The source code for this class is contained in the text area below. You can copy and paste it into your own source code.

Before using the source code, make sure that you read and agree to the terms of the License Agreement, which will open in a separate window.

Comments

This class is intended to be a lightweight browser sniffer. The goal is to provide the most common types of information needed, for the most commonly used browsers.

Legacy techniques for testing browsers often didn't acknowledge browsers other than Internet Explorer or Netscape. Because of this, newer browsers often disguise themselves as either Internet Explorer or Netscape so that they will function properly with legacy code. For this reason, CJL_BrowserSniffer is especially defensive when determining browser type.

Both isIEWin() and isIEMac() check for the existence of the window.external method, which is proprietary to Windows and Macintosh Internet Explorer, starting in version 5.0, in order to more definitively confirm that it is an Internet Explorer based browser.

The Netscape recommended method for determining whether a browser is in the Gecko family, is the window.navigator.product string. Safari deceptively uses this property to disguise itself as a Gecko. There is no legitimate reason to do this, because the property is so new that legacy browser sniffing techniques don't know about it. This is what happens when you, as Apple says, "Think Different." Although Opera does not currently engage in this deception, we have decided to proactively guard against the future misuse of this property by Opera and any future browsers we support, by using the window.navigator.product property in conjunction with the window.navigator.userAgent property in order to more definitively determine whether the browser is an authentic Gecko.

Class Index

Constructor: CJL_BrowserSniffer

CJL_BrowserSniffer()

Description:

Creates a new CJL_BrowserSniffer class for obtaining information about the browser being used.

Methods

isOpera()
Returns true if the browser is Opera or false if it is not.

isSafari()
Returns true if the browser is Safari or false if it is not.

isGecko()
Returns true if the browser is in the Gecko family of browsers or false if it is not. Browsers in the Gecko family include Netscape 6.1 and above and Mozilla browsers.

isIEWin()
Returns true if the browser is Internet Explorer for Windows or false if it is not.

isIEMac()
Returns true if the browser is Internet Explorer for Macintosh or false if it is not.

getVersion()

Returns the browser specific version as a Number Type.

Note that for Gecko family browsers, the version returned is the Gecko specific Release Version.

Support

If you have any questions, find any bugs, or have suggestions, feel free to contact us.