This page uses a js include file. If it is loaded, you should see the phrase, "Dialog.js include file is loaded ..." directly above this paragraph. If you try to use the browser refresh button, it may not reload include files and the page will error out in a big way, particularly if some of the page is written by include file. This is a real trial when debugging. The 'Reload Me' button will completely replace the page, including the js file(s). This problem does not appear on served files -- only when loading local pages. It may or may not be peculiar to Communicator 4.05 and is not a problem with IE 4.01.

   


What is 'eval()'

The eval() function is probably the least used and least understood functions in Javascript (jscript). However, it is this function which provides the real power of the language-- so much so that much of Microsoft's remote scripting functions are written in JScript and eval() has now been added to VBScript.

Simple Calculator

Enter a valid mathematical expression below:


Answer: 

Eval(something) can, as its name suggests, evaluate a mathematical expression passed to it as a string. Try the example at the left.
Here is the function:
function doAlert(expr)
{
   var astr
   astr = eval(expr)
  document.mathFrm.ansTxt.value = astr
}
Simple Alerter
Enter phrase to see in an alert box
Do NOT use quotes, semicolons or hit enter.
Use \n for line breaks



However, eval() has much more powerful capabilities. It can also evaluate a string for valid javascript code and execute it. In this regard, it is very much like the Mac Hypercard 'do' command, which can execute code contained in field or variable.
The example on the left is very simple. The script executed by the onclick event of the button is a little more complex than necessary for an alert script, but, in fact, illustrates the use of eval to EVALuate code.
Here is the function:
function doAlert(expr)
{
  var mstr = 'alert("' + expr + '")'
  eval(mstr)
}
Virtual Variables
Enter a variable name (one word)
The value of the variable will be set to 'green'



Well if that is all that it can do, we are not impressed. onclick="alert(alertInput.value)" would have done as well and not been bothered by control characters in the text string.

However, eval() can do much more esoteric things
One of its most powerful features is the ability to construct virtual variables; that is, the NAME of the variable, given as a string can be converted into an ACTUAL variable and given a value. You cannot do this in VB, Delphi, or C.
Here is the function:
function doVar(expr)
{
  var mstr = varName + ' = "green" '
  eval(mstr)
  alert('The value of ' + varName +
    ' has been set to ' + eval(varName) )
}
  • Line 1 takes the variable name which you gave it and constructs the string of code to execute
  • Line 2 actually executes the code
  • Line 3 writes an alert which shows you the variable name and its value. Note that since eval was used to create the variable, it has to be used to extract the value of the variable.
Virtual Arrays

Enter the name of your menu (one word array name)
Example: Mathematics

Enter the menu (array) contents as a string of ONE WORD entries separated by commas. Do not put any commas or control characters within the string.
Example: Addition,Subtraction,Multiplication,Division

Although the previous example was simple, in itself it is not very useful. In this example, though we will take the virtual variable concept much farther and create a virtual array.

We will do this in the context of building a simple menu-building application. When you click the button, an array will be built which is named whatever you specify. This name will be placed in element 0 of the array. The contents string will be broken up and each phrase placed in a succeeding array element. The array is then handed to another function which uses the array to actually build a simple dropdown. Needless to say, the menu items do not do anything in this simple example.

   

The dropdown is presented as a dialog by Netscape's dialog API. If you want to use it, you must include dialog.js in your page. To see how to use it, also view and download the Netscape dialog widget page. If you use view source on the page, you will see how to use dialog.js. Remember that this code is ©Netscape.
Here is the function which makes the array:
function doArray(aname,values)
{
1 var astr,mstr,crntstr
2 var l_aname
3 var ii
4 var splitArray
5 splitArray = values.split(',')
6 l_aname = splitArray.length
7 mstr = aname + " = new Array(" + (l_aname) + ")"
8 myArray = eval(mstr)
9 for( ii = 1 ; ii <= l_aname ; ii++ )
10 {

11 mstr = aname + "[" + ii + "] = splitArray[" + (ii-1) + "]"

12 eval(mstr)

13 }

14 mstr = aname + "[0] = '" + aname + "'"
15 eval(mstr)
16 var astr = drawSimpleMenuFromArray(eval(aname))
17 var dialog = new Dialog();
18 dialog.setDialogTitle("Menu Dialog")
19 dialog.setDialogBgColor("#F0F8FF")
20 dialog.setDialogPageX(300 + self.pageXOffset)
21 dialog.setDialogPageY(50 + self.pageYOffset)
22 dialog.setDialogContent(astr)
23 dialog.addDialogButton("OKlabel", "   OK   ", "center", 3)
24 dialog.openDialog('myDialog')
25 }
  • Lines 1-3 declare some variables
  • Line 4 declares an array to temporarily hold the values which you passed
  • Line 5 constructs an array of the values which you passed it as a comma delimited string.
  • Line 6 gets the size of the array
  • Line 7 constructs the line of code as a stringwhich will be passed to eval to be executed as code.
  • Line 8 sets 'myArray' to the new array created by eval
  • Lines 9,10 set up a for statement which will iterate through the values in splitArray and set the values of the virtual array to the menu items
  • Line 11 constructs the string for each item
  • Line 12 evaluates the line
  • Line 13 closes the for loop
  • Line 14 constructs the code string to set element 0 to the name of the array
  • Line 15 executes the code string
  • Line 16 calls the function which will build a string that will present the array as a dropdown. Note that it has to pass the virtual array so it has to evaluate aname to actually get the array
  • Lines 17-24 build the dialog using functions from dialog.js

Netscape NOW!