User Agents

Many of the programs on your computer have a user agent that they provide to other programs when they are asked to identify themselves. For web browsers there are in fact two different user agents that we might be interested in. The first of these is the user agent for the web browser itself and the second is the user agent for the operating system. In fact in many cases the first of these user agents is built to contain information that the web browser determined by looking up the user agent for the operating system. The browser user agent can then be accessed in turn from Javascript by referencing navigator.userAgent.

"Doesn't Javascript gives you the browser name in the navigator.appName field?" you may ask. Well, the answer is that it does and it doesn't because some browsers allow you to set them so that they will identify themselves as a different browser than they really are. This is to allow browsers such as Opera 7 to function with web sites that only cater for Internet Explorer or Netscape in the way that the site is coded. Opera 7 can handle just about everything that IE6 or Netscape 7 can and so by changing the browser identification the browser is able to access sites that restrict functionality if you are not using the owner's preferred browser. This is called spoofing. The useragent is also changed to spoof the other browser however, the true browser identification is usually also included in the content of the user agent field. This means that if we need to determine the true identity of a browser we can usually find out from the user agent field even when the browser is set to spoof a different browser. This is useful if we are trying to collect statistics on the browsers that people are using to access our page as we can use the useragent field to distinguish (for example) between IE6 and Opera 7 pretending to be IE6.

Now let's take a look at a few examples of useragents.

As you can see, there is quite a bit of information in some user agents and not much in others. With the information provided we can in most cases correctly identify the browser that is being used although there are a few rather uncommon browsers that identify themselves as something completely different and don't place anything at all in the user agent to identify what they really are. With these browsers we have no option but to take the content of the user agent field at face value and identify the browser as what it is pretending to be rather than what it really is. As such browsers are only rarely used this shouldn't distort our statistics too much.

What is not so obvious is that the operating system information provided in the user agent (in those cases where it is supplied) is not necessarily as accurate as we would like. For example, the more modern browsers will report Windows XP either as Win XP or Win NT 5.1 and will report Windows 2000 as either Win 2000 or Win NT 5.0. If the browser version came out before Windows XP was released then it may not recognise the Windows XP user agent and may report the operating system as Win NT 5.0 which is the latest version that it does recognise. If someone is using a really early browser version then the operating system identification becomes even less reliable.

So now let's see what we can do to construct a Javascript that will interrogate the user agent to determine which browser someone is actually using that we can use to collect more accurate browser statistics. How about we code it like this:

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;

This script starts by converting the user agent to lower case to make it easier to check. It then checks for common (and not so common) browser names in the string starting with more common ones but leaving Internet Explorer, Netscape, and Mozilla until last. Then if we identify as Internet Explorer, we know that it is not a different browser pretending to be IE. We then check if it is a Netscape version 6 or greater. If it isn't and the string starts Mozilla/5.0 then we know that it is a Mozilla version. If the string starts with Mozilla and got this far then we have an earlier version of Netscape. Anything else we assume that the browser name is the first part of the string up to the first slash (if there is one) or the first blank if there isn't a slash. This covers all of the browsers that identify themselves by name in the user agent without having to add the code for each.

With the supplied code this routine identifies the browser you are using as the following:

If you are using a browser that doesn't correctly identify itself with this script, please let me know what browser you are using and the contents of your useragent and I will update the script accordingly.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow