JavaScript Version Incompatibilities

Specifying the JavaScript version

You can specify the level of JavaScript required to execute a script using the language attribute. If a browser does not support the requested version, it should ignore the script. This technique is commonly used to hide advanced features in JavaScript from older browsers which do not support them. However, there is a compatibility problem which can cause problems when JavaScript1.2 is specified as the language in Mozilla-based browsers.

To illustrate, you can use the language attribute to determine the level of javascript your browser claims to support.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Determine JavaScript Version</title>
    <style type="text/css">
      html, body { margin: 0; padding: 0}
    </style>
    <script language="javascript">
      var jsversion = 1.0;
    </script>
    <script language="javascript1.1">
      jsversion = 1.1;
    </script>
    <script language="javascript1.2">
      jsversion = 1.2;
    </script>
    <script language="javascript1.3">
      jsversion = 1.3;
    </script>
    <script language="javascript1.4">
      jsversion = 1.4;
    </script>
    <script language="javascript1.5">
      jsversion = 1.5;
    </script>
    <script language="javascript1.6">
      jsversion = 1.6;
    </script>
    <script language="javascript1.7">
      jsversion = 1.7;
    </script>
    <script language="javascript1.8">
      jsversion = 1.8;
    </script>
    <script language="javascript1.9">
      jsversion = 1.9;
    </script>
    <script language="javascript2.0">
      jsversion = 2.0;
    </script>
  </head>
  <body>
    <div>
    <script language="javascript">
      document.write('Your browser claims to support JavaScript ' + jsversion)
    </script>
    </div>
  </body>
</html>

Internet Explorer 6 claims support for JavaScript 1.3 while Mozilla, Mozilla Firefox and Opera 7.54 claim support for JavaScript 1.5. Konqueror 3.2 appears not to respect the language attribute and therefore claims to support JavaScript 2.0 which is not even defined yet.

(Launch Example)

JavaScript Version and Browser Implmentations

The history of JavaScript versions and browser implementations is really a short history the Browser Wars of the 1990's.

Netscape Navigator 2.0 introduced the JavaScript language and the ability to perform client-side scripting in browsers. Microsoft introduced Internet Explorer 3.0 which emulated much of the implementation of JavaScript in Navigator 2.0.

Netscape countered by introducing a new version of JavaScript 1.1 in Navigator 3.0. Netscape submitted JavaScript 1.1 to the ECMA for standardization as ECMAScript ECMA-262 Edition 1.

As ECMAScript ECMA-262 Edition 1 was being finalized, Netscape released Navigator 4.0 with a new version JavaScript 1.2 which turned out to not be compatible with the finalized ECMA-262 Edition 1 standard. Shortly thereafter Netscape released Navigator 4.06 with JavaScript 1.3 which resolved the incompatibilities with the ECMAScript ECMA-262 Edition standard.

In order to provide authors a means of supporting scripts which had been written for Navigator 4.0-4.05, Netscape used the language attribute JavaScript1.2 in Navigator 4.06 and later to switch behavior from the ECMAScript standard to the non-standard behavior expected by Navigator 4.0-4.05. Mozilla continues to use JavaScript1.2 as a compatibility switch.

JavaScript1.2 is not your friend

If you specify JavaScript1.2 as the value of the language attribute in a SCRIPT tag, you will change the behavior of Mozilla's implementation of JavaScript. Some of the problems you may find in JavaScript1.2 compatibility mode are:

Arrays

In JavaScript1.2 mode, var a = new Array(3) will create an array with a[0] = 3 while in JavaScript 1.3 and later a will be an array with length of 3.

Equality Comparisons

In JavaScript1.2 mode, == and != will be treated as strict equality comparison operators (=== and !==). See Comparison Operators in the DevEdge Core JavaScript 1.5 Guide for details concerning the difference between Equal (==) and Strict equal (===), and between Not Equal (!=) and Strict not equal (!==).

Examples

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Determine JavaScript Version</title>
    <style type="text/css">
      html, body { margin: 0; padding: 0}
    </style>
  </head>
  <body>
    <script language="javascript">
      var arr = new Array(3);
      var foo;
      if (foo == null)
      {
        foo = 'bar';
      }
      document.write('<p>arr.length is ' + arr.length + ', arr.toString() is ' + arr.toString() + '<\/p>');
      document.write('<p>foo is ' + foo + '<\/p>');
    </script>
    <script language="javascript1.2">
      var arr12 = new Array(3);
      var foo12;
      if (foo12 == null)
      {
        foo12 = 'bar';
      }
      document.write('<p>arr12.length is ' + arr12.length + ', arr12.toString() is ' + arr12.toString() + '<\/p>');
      document.write('<p>foo12 is ' + foo12 + '<\/p>');
    </script>
  </body>
</html>

Mozilla will output:

arr.length is 3, arr.toString() is ,,

foo is bar

arr12.length is 1, arr12.toString() is [3]

foo12 is undefined

While Internet Explorer 6, Opera 7.54 and Konqueror will output:

arr.length is 3, arr.toString() is ,,

foo is bar

arr12.length is 3, arr12.toString() is ,,

foo12 is bar

Your browser says:

(Launch Example)

Recommendations

Review your code for instances where you may have occurences of array initializers or comparisons whose results differ between normal equality comparison operators and strict equality operators. I have added the ability to flag uses of JavaScript versions in script language attributes to the combinedhooks.js script in Mozilla Spiders. By scanning your content using Mozilla Spider, you will be able to quickly determine where you may have problems due to the use of JavaScript1.2 compatibility mode.

See Also

home | up | topabout: