New Revised CSS Library Forums Web Tools
FAQs Awards Usage Terms Contact
Categories
Other Sections
Sweet Ads
Compatibility
Bookmark online:


FF1+ IE5+ Opr7+

Browser Sniffer Script (version 1.1)

Note: Updated Jan 31st, 05' to include recognition of Opera 8

Description: This browser sniffer detects all the major browsers currently out there, and passes the results on as variables to facilitate your browser detection needs. It differentiates between Windows, Mac and Linux, and the most commonly used DHTML browsers - IE4/5/6, Netscape 4/6/7/other Gecko, Konqueror 2.2+, Safari, Opera 5/6/7. All other browsers are grouped together under a single exclude variable, to protect them from complex scripting.

Demo: n/a


Directions: Simply paste the following script at the beginning of the <HEAD> section, so that other scripts on the same page can have access to its variables. If you use it on more than one page, consider saving the script as an external .js file, so that it caches:

Select All

Variables from the script

The script returns a set of browser variables (Below are live results dependant on your browser type):

    var returns Description
    ie Internet Explorer 4+ and IE-based third-party browsers. You can also be more specific:
    ie4 ... Internet Explorer 4 only.
    ie5 ... Internet Explorer 5 or 6.
    ie6 ... Internet Explorer 6 only.

     
    ns4 Netscape 4

     
    ns6 Gecko and KDE-based browsers - which includes Netscape 6 and 7, Mozilla, Konqueror and Safari. You can also identify smaller groups within this:
    ns7 ... Netscape 7.
    mz7 ... any gecko browser except Netscape. This is principally designed to identify Mozilla's own builds from Version 0.6 onwards, but it also returns true for any other non-netscape gecko browser.
    kde ... Konqueror, from KDE 2.2 onwards.
    saf ... Safari. This variable will identify Safari irrespective of which browser it's set to identify as.

     
    op5 Opera 5
    op6 Opera 6
    op7 Opera 7

    These variables will identify Opera irrespective of which browser it's set to identify as.

Underpinning these is a safety variable, for protecting legacy browsers:

    exclude

There are also three OS variables:

    win Windows
    mac Mac OS
    lin Linux, or anything else

and you can query a lower-case version of the user agent string:

    agt

The sniffer variables are global, and therefore available to any other scripts on the same page. They allow you to code for or exclude specific browsers, eg:

if (mac&&ie5) { ... internet explorer 5 on a mac ... }
if (!ns4) { ... not netscape 4 ... }
if (win&&(op5||op6)) { ... a windows version of opera 5 or 6 ... }
if (ie5||ns6||op7) { ... ie5+, gecko, kde or opera 7 ... }

Remember that some browsers return true for more than one variable:

  • Explorer 6 returns true for (ie6) and also for (ie)
  • Explorer 5 or 6 return true for (ie5) and also for (ie)
  • Explorer 4 returns true for (ie4) and also for (ie)
  • Konqueror returns true for (kde) and also for (ns6)
  • Safari returns true for (saf) and also for (ns6)
  • All Gecko-based browsers which aren't Netscape return true for (mz7) and also for (ns6)

The exclude variable returns true for all unspecified browsers. This is very useful for protecting them from scary DHTML, eg:

if (!exclude) { ... modern browsers only ... }

You may find it necessary to use a combination of expressions to get to the browser you need, eg:

if (!exclude) {
    if (ns6||op7) { ... gecko, KTHML or Opera 7 ... }
    else if (ie5) { ... internet explorer 5+ ... }
    else { ... any other dhtml browser ... }
    }

There are several minor browsers which have enough DHTML support to pass one of the object tests, but not enough to be useful. The script specifically filters out (adds to the exclude group) - Espial Escape, ICEBrowser and OmniWeb.