Which Browser is That

Detecting which browser the visitors to your site are using is useful for statistical purposes in helping you to work out which older browsers are worth the wextra effort of continuing to support. By this I mean where you take extra steps to try to work around those standards that a given browser doesn't support so as to have that older browser behave as close as possible to the way that more modern browsers behave in how they display your page. You should of course continue to support even older browsers to the extent that your page content is still readable even in the earliest of browsers.

So how do we detect which browser our visitors are using? Well the easiest way is probably to look at the useragent that the browser supplies. Now this field can be changed by the browser owner to contain just about anything at all but the majority of our visitors will leave the value set to its default value where it does actually identify which browser it is that they are using rather than misidentifying itself as a different browser in order to bypass badly written code that ignores certain browsers. There will be a percentage of browsers configured to identify themselves as a different browser but provided that we are only using the information for statistical purposes we can reasonably assume that the figures we get for the usage of the different browsers are within say 10% of the correct figure. This is accurate enough for determining those browsers where you have sufficient visitors using the browser to make it worthwile to put in the extra effort in getting the page fully functional.

Identifying the specific browser where we have no margin for error is more difficult. To start with, there does not appear to be any information regarding which browser that it is passed to the server directly apart from the useragent. This means that any more accurate browser detect will need to rely on our visitor having JavaScript enabled in order to have it attempt to identify the information and then pass it back to the server. The other problem is that very few browsers actually provide a means of positively identifying the browser even with JavaScript.

There are in fact only two of the major browsers where there is any way of positively 100% confirming that the browser being used is a specific browser where javaScript is available to do the test. The two browsers where it is possible to positively identify that it is that particular browser and no other are Internet Explorer and Opera.

Internet Explorer can be positively identified due to the fact that it is the only browser that runs JScript rather than JavaScript and therefore it supports jScript conditional comments. Opera can be positively identified because it actually creates one additional object that no other browser creates specifically for the purpose of making its identification possible. We can therefore run the following code that will set one of three values depending on which browser is being used.

var browser = 'Not IE or Opera';
/*@cc_on
@if (@_jscript) browser = 'Internet Explorer';
@else */
if (window.opera) browser = 'Opera';
/* @end @*/

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate