BAPS: Browser and plugin sniffer
Browser and plugin sniffer (BAPS) is a tool to detect factors regarding a user's environment, such as browser, version, language, plugins and more. Browser sniffing is famously fiddley due to a lack of consistent implementation by browser vendors (until recently, even jQuery had given up trying to consolidate this with its $.browser object). This script aims for reliable detection. It sets a global variable, baps, an object through which the various pieces of data can be accessed.
Here's the code behind that example:
3for (var i in baps)
4 document.write('<li>'+i+': '+baps[i]+'</li>');
Simply call the script into your page. It will set a global variable,
browser - the real name of the user's browser (e.g. Chrome - not Netscape!)
browserVer - the version of the user's browser. See notes.
language - the user's language. See notes.
cookiesEnabled - whether or not the user has cookies enabled
platform - a string pertaining to the user's OS, e.g. 'MacIntel' or 'WinNT 5.1'. See notes.
...and then some more info regarding the major plugins, if installed:
flash - the version of Flash Player plugin installed
wmp - the version (IE) or presence (non-IE) of the Windows Media Player plugin installed. See notes.
quickTime - the version of the QuickTime plugin installed
BAPS adds the following classes to the body in order to help your CSS:
- browser - the name of the browser the user is using
- apVer-N - the version of the browser the user is using, where N is the version
- apVer-N - as above, but not including sub-version
- platform-N - the platform of the user's computer, where N is the platform
Inspect the DOM on this page to see the classes BAPS has added.
BAPS can easily be extended to sniff for more plugins. Check out this very helpful page for how to do this for the most common plugins.
The inconsistent, murky world of browser sniffing therefore guarantees quite a few caveats...
Sometimes browsers expose two versions to JS - the release version and the build version. For example, Opera 11.52 actually tells navigator.appVersion that it's 9.8, becuase that's the version of the build that 11.52 is bundled from. Since, as a developer, you will nearly always want to know the 11.52 and not 9.8 (11.52 is exposed to navigator.userAgent, by the way), BAPs uses the latter for browserVer.
One of the greatest inconsistencies in how browser vendors approach the navigator object is to do with language. Browsers variously define language, userLanguage, userLanguage and sysLanguage. Where supported, language is used (but Opera, for example, also supports userLanguage).
The navigator.platform property is not overly useful as it doesn't normally give you the name of the OS release - only its build. Windows XP, for example, comes up as 'NT 5', whereas Mac Snow Leopard is simply MacIntel.
With plugin detection, it's a case of IE vs. the rest. IE exposes info about installed plugins not through navigator.plugins but through MS proprietary ActiveX. One issue with this is you cannot detect WMP version except in IE - only whether it is installed or not.