Javascript code to identify your browser

Note: Using this code will mean your page would have to be validated as HTML4 Transitional because the language="" attribute is not part of HTML4 and will cause validation of a HTML4 Strict file to fail. The need for this type of code is diminishing with the increasing support for CSS1 and CSS2.

Each browser identifies itself by means of a navigator object. This object is accessible through all versions of Javascript. To use the identity of the browser to redirect the user to the appropriate page, we need to store the identity of the browser as a variable:

<script type="text/javascript">
var isNew=0;
var isNS4=0;
var isIE4=0;
var isIE5=0;
var isNS3=0;
var isIE3=0;
//  -->
</script>

These variables need to be accessed by all browsers. However, each browser can only access javascript up to the version it can understand, therefore early browsers will ignore all javascript code marked as of a later version - even if the code itself is an earlier version. So if you need to exclude IE3, Netscape 2 and Netscape 3 from the next section of code, specify Javascript1.2 (note, that the language attribute is no longer part of HTML4 - validate as HTML4 Transitional if you need to use this to exclude certain browsers in this way.)

<script language="Javascript1.2" type="text/javascript">

Now we address the navigator object and ask for the name of the browser. The same line of code looks for a version number in the navigator.appVersion data and appends that to the browser name

var brow=((navigator.appName) +
(parseInt(navigator.appVersion)));

Now we test for which browser is in use and store the result.

if(parseInt(navigator.appVersion >=5)) {
if(brow == "Microsoft Internet Explorer5") isIE5=1;
else isNew=1;
}
else if(brow == "Netscape4") {isNS4 = 1;}
else if(brow == "Microsoft Internet Explorer4") {isIE4=1;}

Netscape 3 has not yet been identified, so we specify Javascript1.1 to exclude IE3 and Netscape 2.

Why exclude?

<script language="Javascript1.1" type="text/javascript">

Finally, it's back to Javascript (version 1 as it's not specified) to find IE3. Work on this code yourself, but note that there is a bug in IE3 - it reports the name as Microsoft Internet Explorer but reports the version as Mozilla 2. Therefore, the code used above will return

brow="Microsoft Internet Explorer2".


This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.