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.
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
|
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) ) } |
|
|
Virtual Arrays |
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 } |
|