DHTML Lab - dhtmlab.com - Hierarchical Menus Ver. 3 - Addendum IV (v3.04) | 3
Hierarchical Menus Ver. 3 - Addendum IV (v3.04)
the Navigator 4.5 eval() bug
Consider this function:
function doIt(arg) { arg = 5; eval("newVar = arg"); document.fmArg.taArg.value += "arg is: "+ arg +"; newVar is: "+ newVar +"\n"; }
The function doIt() is passed one argument, arg. Regardless of the passed value of arg, it is reassigned a value in the first statement:
arg = 5;
Now, it has a value of 5. All consequent function statements should work with this new value. Our second statement uses eval() to evaluate a string that assigns the value of arg to the newVar variable:
eval("newVar = arg");
When the statement is executed, newVar is created and should have a value of 5.
The final statement displays the values of arg and newVar in a text area, for demonstration purposes.
The form below uses the sample function, passing three different values for arg: 3, no value and null. Try it with Navigator 4.5 if you can, and with any other JavaScript-enabled browser. Compare the results.
For those of you without access to many browser versions, here are three screenshots of the results. The three form buttons above have been clicked in order (top-to-bottom):
Browser | Results |
---|---|
Navigator 3/4: All versions of Navigator give the correct result. | |
Explorer 4: Explorer also produces the expected display. | |
Navigator 4.5: Alas, the newVar variable is not assigned the correct value. |
In all cases, arg has a value of 5. However, when arg is used in eval(), in Navigator 4.5, it has a value of 5 only if it had a value to begin with! If it had no value (undefined) as a function argument, it retains that value throughout the function, regardless of any new assignments. Consequently, if arg was originally undefined, newVar will also be undefined.
Compare our example to these lines from makeElement():
if (!whichContainer) whichContainer = menuLoc; eval(whichEl + "= new Layer(elWidth,whichContainer)");
The first statement reads, in plain English:
If whichContainer is false, or null, or 0, or undefined, then assign the value of menuLoc to it. As seen on the previous page, we omit the whichContainer argument when we are building a menu element (as opposed to an item element). whichContainer is then assigned the value of menuLoc, correctly. In the next statement, whichContainer is used in eval(), and the Navigator 4.5 bug kicks in: whichContainer retains its undefined value, since no value was originally passed as an argument.
The Fix
Whenever makeElement() is called, we must ensure that all arguments have a value. We simply pass null values, for arguments we had previously omitted:
function makeMenuNS(isChild,menuCount,parMenu,parItem) { . . . if (!isChild) { tempWidth = tempArray[0] ? tempArray[0] : menuWidth; menu = makeElement("elMenu" + menuCount,tempWidth,null,null); } else { menu = makeElement("elMenu" + menuCount,null,parMenu,null); } . . . }
Next, let's fix another problem we've been having with Navigator.
Produced by Peter Belesis and
All Rights Reserved. Legal Notices.Created: Dec 20, 1998
Revised: Dec 20, 1998
URL: https://www.webreference.com/dhtml/column21/addendum4/col21addIV3.html