JAVASCRIPT UTOPIA  back to
WRITING BACKWARDS COMPATIBLE CODE  


okay, so i'm getting a little sick of all the browser sniffing that's going on inside of javascript code just to work-around objects, methods, and properties that aren't supported everywhere. people seem to be thinking "that object/method/property isn't supported by IE3, so i'll just sniff IE3 (or whatever browser) and execute different code for it". that's a pretty dangerous habit to get into. suppose, for instance, you sniff IE3 out of your code because it doesn't support a feature included in javascript1.1 but the user of IE3 has the upgraded jscript .dll which brings its javascript compatibility up to version 1.1? that user won't see your stuff even though they could have.

the deal is that, wherever possible, writing backwards compatible javascript code shouldn't include browser-specific handling, but should really involve checking language version for feature support. if the code doesn't work because the object/method/property isn't recognized it's likely that the code is expecting something that's only available in a certain version of javascript and the browser doesn't support that version.

since we can easily look up the versions at which every object/method/property entered the language, the best approach to writing backwards compatible code is to detect not the browser, but the level of javascript support. once the level of javscript support is determined, then any code can be executed conditionally based on that level--with confident knowledge that (no matter what the browser) the objects, methods, and properties used will be supported.

detection of javascript version seems surprisingly under-used considering how easy it is, and how stable it can make code. for those of you who haven't seen this technique, here's some sample code to show you how to do it (it's the code that called the alert when you accessed this page):

the code
quick summary: initialize a global variable called javascript_version inside a SCRIPT tag that has its LANGUAGE attribute set to "JavaScript", which is accessible to all javascript-capable browsers. then update that global variable in other SCRIPT blocks that are not readable by browsers that support a level of javascript less than the version indicated in the LANGUAGE attribute. by the time the browser reaches the end of the chain, the javascript_version var will be set to the level of javascript it supports.


<SCRIPT LANGUAGE="JavaScript">
<!--
var javascript_version = 1.0;
//-->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.1">
<!--
javascript_version = 1.1;
//-->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript1.2">
<!--
javascript_version = 1.2;
//-->
</SCRIPT>


<SCRIPT LANGUAGE="JavaScript">
<!--
if(javascript_version==1.0)
	{
	alert("your browser supports javascript 1.0");
	}
else if(javascript_version==1.1)
	{
	alert("your browser supports javascript 1.1");
	}
else if(javascript_version==1.2)
	{
	alert("your browser supports javascript 1.2 or higher");
	}
//-->
</SCRIPT>



Revision History

  1. May 7, 1999: Posted.