mkaz.com home photography web dev personal about

JavaScript : Browser Versioning


There are several different browsers and versions out there, thus you, the programmer, do not know what JavaScript capabilities the user has. Insuring that the scripts you write run in most users browser can be done in a few different ways.

One way is to ask the user what browser they are using, via JavaScript, and to then program accourdingly. This is the most common way to avoid any scripting errors on the different browsers.

The information at the top of the page was printed out using the following functions, which retrieve the browser version and name:

<SCRIPT LANGUAGE="JavaScript">
document.writeln(parseInt(navigator.appVersion));
document.writeln(navigator.appName);
</SCRIPT>

This works for both Netscape's Navigator and Microsoft's Internet Explorer. The Navigator name is part of the function because Netscape created JavaScript. Here are some examples have how these can be used:

<SCRIPT LANGUAGE="JavaScript"><!--

var iVer = parseInt(navigator.appVersion);
var sName = navigator.appName;

if (iVer >= 3) { ...perform some function... }

if (sName == "Netscape") { ... do this ... }

else if (sName == "Microsoft Internet Explorer")
{ ...do something else... }

//-->
</SCRIPT>

 

You can also use this when calling functions within the body part of the document. For example:

<FORM>
<INPUT TYPE=button
onClick="if (iVer >= 4) doThisFunction();">
</FORM>

 

However the above way requires you to know which specific functions and methods are supported by which browser, and which version. The following two ways try and get around that.

A way of trying to version detect your scripts, is to use the actual script tag. You can specify the version in the LANGUAGE value. The browsers which support the level of JavaScript specified will run the script, if not supported it should not run it. This feature is only supported by Netscape. Examples:

<SCRIPT LANGUAGE="JavaScript1.0"> <SCRIPT LANGUAGE="JavaScript1.1"> <SCRIPT LANGUAGE="JavaScript1.2">

Netscape ver 2.02 supports JS ver 1.0, Netscape 3.0x supports ver 1.1, and you could probably guess that Netscape 4.0x supports JavaScript ver 1.2.

 

However that way still requires you to know which functions came out in what version, and it only works on Netscape, which is good enough for me, but not everyone.

Another way of trying to insure your scripts run fine, is not to check what browser is used, but to check if the JavaScript object is defined. You can do this with a simple if statement.

For example, in 2.0 browsers, images are not defined as objects in JavaScript, thus the JavaScript object document.images is not defined. Undefined is considered to be false, such as in an if statement.

if (document.images) { ...document.images is defined }

That is all there is to it. The drawback to using this method is that you may have several if statements checking several different objects at once.

It is always best to check your code in numerous different browsers, with numerous different settings.

 


Related Links: