GSAnet Banner Swap





to tricks page

How do I check for the browser version?

There will be a time in your designing when you start using scripting or HTML functions that are not supported by all browsers. And this time will come very soon.

A practical example would be a document.images array (instructions on how to use document.images will be posted here very soon). Document.images array is supported by Netscape Navigator 3.0 but not by MS IE 3.0. What's even worse is the fact that if you write a script for Navigator 3.0, it will crash if a user views your page with MS IE 3.0.

All right, even if you are unsure why you would need to check versions, for now just remember that you can do it and that you will need to do it to be able to enable backward-compatibility with the browsers.

The following code shows you how to check for browser versions.

<SCRIPT language="JavaScript">

browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);

if ( browserName == "Netscape" ) {
if ( browserVer < 2 ) { version = "NN1"; }
if ( browserVer == 2 ) { version = "NN2"; }
if ( browserVer == 3 ) { version = "NN3"; }
if ( browserVer == 4 ) { version = "NN4"; } }

if ( browserName == "Microsoft Internet Explorer" ) {
if ( browserVer < 2 ) { version = "MS1"; }
if ( browserVer == 2 ) { version = "MS2"; }
if ( browserVer == 3 ) { version = "MS3"; }
if ( browserVer == 4 ) { version = "MS4"; } }

</SCRIPT>

Explanations:

It is all based on two variables from the navigator object. Navigator.appName gives the name of a broswer and Navigator.appVersion gives a version in string format. The appVersion is then converted into an Integer by using ParseInt() procedure.

The versions are checked by the if commands and the result is written into the version variable, which is also in string (character) format. You can later test the versions by if ((version == "MS4") || (version == "NN3)) { list of commands to do }. And of course the code can be modified, because I don't think you will ever need to differentiate between MS IE1 and MS IE2, because no one ever used them.


©1997 repfect Drug design studio. All Rights Reserved.