Hiermenus Go Forth, XX - DHTML Lab | 8 | WebReference

Hiermenus Go Forth, XX - DHTML Lab | 8

Logo

Hiermenus Go Forth, XX:
Version 4.0.13 - The Complete Script (Full-Window)



Incorrect Array Syntax

Many have written to complain about the following error message in IE:

What has baffled the writers even more is that the Netscape browsers (4 and 6) did not generate an error on the same page.

The cause of the error is incorrect menu array syntax. An extra trailing comma has been inserted into the array:

Here is a correctly formulated menu array:

HM_Array1 = [
[120,"mouse_x_position+15",,"black",
"white","white","black","black","gray"],
["Experts","https://www.webref.com/experts/",1,0,1],
["Contents","https://www.webref.com/index2.html",1,0,0],
["Services","https://www.webref.com/index2.html",1,0,1],
["About","https://www.webref.com/about.html",1,0,0]
]

And here is an incorrect one:

HM_Array1 = [
[120,"mouse_x_position+15",,"black",
"white","white","black","black","gray"],
["Experts","https://www.webref.com/experts/",1,0,1],
["Contents","https://www.webref.com/index2.html",1,0,0],
["Services","https://www.webref.com/index2.html",1,0,1],
["About","https://www.webref.com/about.html",1,0,0], 
]

Everyone knows that the comma separator should not be at the end of the array. Most of you who have encountered the error would have found the cause of it earlier if Netscape also generated an error. JavaScript syntax, after all, is JavaScript syntax. Right?

Yes, most of the time. There are, however, a large number of small differences between Netscape's JavaScript and Microsoft's JavaScript clone, JScript. This is one of them.

In Netscape, a final trailing comma in an literal array declaration is dropped!

In Explorer, a final trailing comma in an literal array declaration is regarded as a bona fide element with a null value!

For example, the following array:

myArray = ["one","two",,"four",];

has different length values for the two browser types:

Netscape: myArray.length: 4
Explorer: myArray.length: 5

Explorer, therefore, thinks there is one more item element than intended and attempts to build an additional menu item without the requisite item parameters. Instead of an item array, it finds a null value and cannot parse it, ergo the error.

Notice that in both browsers the third blank element is considered a valid null element. It is only the final blank element that affects the browsers differently.

I suppose there are arguments for both sides:

Netscape
A trailing comma is illegal when using the new Array() constructor, so it should be illegal in an array literal. Instead of throwing an error, it should be ignored.

Explorer
In an array literal, null elements can be specified by simple comma addition. Therefore a final comma-induced null element is valid.

Whichever argument is more convincing, it doesn't change the fact that the comma should not be there. Since many authors make the trailing comma mistake, usually by cutting and pasting item element arrays, we have decided to have the HM script check for the mistake and to adjust the array, avoiding the error.

The HM script cannot check for all syntax errors. Version 4 has several checks but these always slow down the script. We want to include only the most important checks. Please review your syntax carefully.

On the next page, our second fix.


Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: July 12, 2001
Revised: July 12, 2001

URL: https://www.webreference.com/dhtml/column56/5.html