Browser Detection

Attempting to detect which browser your visitor is running was never a good idea. Using feature sensing to allow your JavaScript to run in any browser that supports the particular features of JavaScript that your script uses has been available since JavaScript was first created and guarantees that if their browser can run it then it will be run. Browser detection on the other hand relied on you being able to both correctly identify their browser and to know which browsers support your code.

With the thousands of different browsers available today and with new browsers being added every day, you cannot cover all the different browsers and cannot hope to keep up with new versions that add the support that allows the new version of a browser to now run your script. To make things even worse, the field the only field that supplied an identification for the browser is the user-agent field which as its name implies can be set by the user of the browser. In fact most popular browsers make it easy to change the user-agent by supplying a built in switcher that allows the user of the browser to select from a list of several different browsers that they want it to identify itself as. Several of the most popular browser even allow adding user-agents to the list they choose from or even allow the field to be set to anything at all.

This latest version of my browser detection script has been amended to take all of these factors into account. The one browser that we can detect for certain is Internet Explorer (at least for version 9 and earlier) as this is the only browser that can run JScript and so we can use a JScript conditional comment to detect that browser. Even this identification becomes problematic when you consider that from version 9 onward it can also run JavaScript and so has abandoned at least some of the JScript differences (the conditional comments are then no longer necessary and may be dropped from IE10). As far as selecting between other browsers is concerned, since the most popular browsers all allow the user-agent to be set to that of most other popular browsers, we can't tell much from the actual value in that field at all - about all we can do is to eliminate those browsers that are unable to set the user-agent to a particular value and so we can identify those browsers that we know are not the one being used.

The script tests for the values that appear in the standard user-agent strings for the five most popular browsers as well as the two most popular mobile device identifiers and reports on which of those browsers and devices are unable to set the user-agent to contain the values it actually does contain.

JavaScript

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate