1 | eval is an important function that lets you evaluate JavaScript code on-the-fly |
2 | For instance, document.forms[0].myName.value is the value of a form element: |
3 | <INPUT TYPE="text" NAME="myName" VALUE="value" onChange="handle(this.name)"> |
4 | this.name holds myName as an ASCII string: |
5 | var x = document.forms[0].myName.value; // No! |
6 | var s = "document.forms[0]." + myName + ".value"; |
7 | var x = eval(s); // Yes! |
8 | That is, eval applies the JavaScript interpreter to its argument |
9 | This can be used to copy information between forms as in |
10 | eval("document.forms[1]." + myName + ".defaultvalue") |
11 | = eval("document.forms[0]." + myName + ".value") |
12 | For a long time, eval didn't work right, so check your browser! |