The last topic I'll take a look at in our coverage of JavaScript functions gives us a look at the functions that are already built in to JavaScript, and are available anywhere in your code. (In fact, these built-in functions are sometimes called global functions.) Table 3.15 lists these functions. Table 3.15. JavaScript's Global Functions
One of the functions in Table 3.15 is particularly powerful eval , which enables you to evaluate JavaScript code. I'll take a closer look at this function here, with an example that enables you to type in JavaScript expressions and evaluate them on-the-fly . Tip Also see the execScript method in Chapter 8, "Using window and frame Methods and Events." Here's the code for that examplein this case, I'm reading the JavaScript expression the user has entered into a text field, evaluating it with eval , and displaying the result in another text field. We're anticipating our work with HTML text fields hereyou might note in passing how this code refers to the text in a text field, which is stored in its HTML value propertyas document.forms[0]. name .value , where forms is a document object array that holds all the forms in the page, and name is the name given to the text field: (Listing 03-17.html on the web site)<HTML> <HEAD> <TITLE> Using the eval Function </TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- function evaluator() { document.forms[0].result.value = eval(document.forms[0].expression.value) } // --> </SCRIPT> </HEAD> <BODY> <H1>Using the eval Function</H1> <FORM> <INPUT TYPE="TEXT" NAME="expression"> <INPUT TYPE="BUTTON" ONCLICK="evaluator()" VALUE=" = "> <INPUT TYPE="TEXT" NAME="result"> </FORM> </BODY> </HTML> You can see the results in Figure 3.15, where I've typed in a JavaScript expression and clicked the = button, which makes the result appear in the second text field. Figure 3.15. Using eval to evaluate a JavaScript expression.
Tip This example, which evaluates JavaScript expressions on-the-fly, can even be a useful tool as you develop your own code and want to check some JavaScript before putting it into your web page. One use for the eval function is to get access to objects whose names you already knowfor example, an eval statement can return a reference to the document object if you pass it the string "document" . What use is that? Well, the two major browsers have different object models, and if you try to execute a statement that uses an object that doesn't exist in the current browser, your script will crash. (Just having such statements in a script doesn't cause a problemit's only if you try to execute those specific statements that your script will crash.) To avoid that, you can check which browser you're working with, and then use eval to get a set of objects legal for that browser by passing eval their names. Here's a simple example that gets a reference to the document object by passing the text "document" to eval (this is just an exampleboth browsers support a document object, of course): (Listing 03-18.html on the web site)<HTML> <HEAD> <TITLE> Using the eval Function </TITLE> </HEAD> <BODY> <H1>Using the eval Function</H1> <SCRIPT LANGUAGE="JavaScript"> <!-- var obj = "document" for (var property in eval(obj)) { document.write(property + ": " + document[property] + "<BR>") } // --> </SCRIPT> </BODY> </HTML> |