The command syntax for the EVAL command is:
What does EVAL do
Reducing the number of lines of code
You can use this command in a variety of activities, for example, if you have a list of fields' names which must contain a value on an HTML form, conventionally you would have had to write an if/then for each field, leading to verbose code, which is not the easiest to read, maintain, or debug, especially within a large and/or complex Web application (See Listing A). Using the EVAL command, you can reduce this to a simple array and loop as shown in this Listing B.
As you can see, you have reduced the size of the code required to check the eight fields from 32 to 10 with no loss of functionality, but with a savings of about 70 percent in terms the of number of lines required. Utilizing the EVAL command has also produced a much more user friendly, maintainable and concise code than the traditional approach.
The real power of EVAL
An example of the use of this command can be seen in the auto-generated function shown in Listing C from MacroMedia's DreamWeaver Editor.
This code sets the location of the object specified by the targ parameter to that of the value attribute of the selObj drop-down; the other parameter simply resets the drop-down if set to true. The reason that EVAL is used here is that it allows the developer to provide the target for the function, which could be a variety of things, for example:
- targ value - load the selected URL
- Document - current window
- Parent - parent document when using Frames/IFrames
- newwin - A different window identified by newwin
- parent.mainframe - a sibling Frame/Iframe identified by mainframe
- Top - The top level item in the current window
As you can see, the range of possible values for this function makes any conventional or hard-coded approach impossible to implement for this functionality.
Doing this without EVAL
Searching the Internet while doing research for this article, I came across a multitude of code samples that used this command. In many cases, there was no other way to implement the functionality that was required, but in some there was an alternative. For example, it is also possible to implement the code in Listing C by using the elements array of the Form object as shown in Listing D.
As both approaches achieve the same end, with about the same amount of code and complexity, which one should you use and why?
To get a reasonable selection of test data, the tests were run three times using IE 6 (SP1), NN 7.1 and Opera 5, using a new browser instance for each test, all on Windows 98 and the tests were run between 01:00 and 02:00 GMT. The results are listed below in milliseconds in the respective order (IE, NN, Opera).
Not only do these results show a significant difference between each of the four approaches, but also a significant difference depending on the browser used. Therefore, browser selection, as much as algorithm selection, could play a significant role when you need to perform a large number of calculations on the client machine.
The second area is interaction with the Document Object Model (DOM). The three main approaches here are:
- Use the EVAL command to create a link directly to the object.
- Use document.all, which returns all matching objects (IE only).
- Use getElementByID, which returns the first match it finds.
A word of caution
Despite the advantages of using EVAL, in some cases, the only major problem is the command itself. It will execute whatever is passed to it, so be very wary of using this command in a situation where you don't trust the users' honesty or ability completely.
Using the command
I have not found a technically compelling argument to use EVAL over other methods for general day-to-day development, although there are exceptions as I have discussed in this article. I would like to hear some constructive feedback from all interested parties to this article—help me refine my understanding on the use of this command.