Archive for August, 2007

Web site traffic - 1053Chapter 39 .Control Structures and Exception Handling Chip:

Sunday, August 26th, 2007

1053Chapter 39 .Control Structures and Exception Handling Chip:  Price: If you need this kind of functionality in your script but your audience is not all running level 4 or later browsers, see Listing 39-1 for ways to simulate the switch statement with if. . .else constructions. Exception Handling The subject of exception handling is relatively new to JavaScript. Formalized in Edition 3 of ECMA-262, parts of the official mechanism are implemented in IE5, with a more complete implementation in NN6. As you see in the rest of this chapter, both IE5+ and NN6 follow many of the same rules with respect to controlling execution paths (the primary subject of this chapter). But IE s departure from the ECMA-262 specification on some of the details can force scripters to take some extra steps to make exception handling work smoothly across browsers. More on that later. First, an overview of exception handling. Exceptions and errors If you ve done any scripting, you are certainly aware of JavaScript errors, whether they be from syntax errors in your code, or what are known as runtime errors errors that occur while scripts are processing information. Ideally, a pro gram should be aware of when an error occurs and handle it as gracefully as possi ble. This self-healing can prevent lost data (usually not a big problem in Web applications) and prevent users from seeing the ugliness of error messages. In Chapter 16, you learn about the onError event handler (and window.onerror property), which were early attempts at letting scripts gain a level of control over runtime errors. This event-driven mechanism works on a global level (that is, in the window object) and processes every error that occurs throughout the page. This event handler ends up being used primarily as a last-ditch defense against display ing any error message to the user and is a long way from what programmers con sider to be exception handling. In the English language, the term exception can mean the same as something out of the ordinary, or something abnormal. This definition seems quite distant from the word error, which usually means a mistake. In the realm of programming languages, however, the two words tend to be used interchangeably, and the differ ence between the two depends primarily on one s point of view. Exceptions
We recommend high quality webhost to host and run your jsp application: christian web host services.

1052 Part IV . JavaScript Core Language Reference (Web hosting bandwidth)

Sunday, August 26th, 2007

1052 Part IV . JavaScript Core Language Reference Listing 39-6: The switch Construction in Action Switch Statement and Labeled Break Branching with the switch Statement


Select a chip for lookup in the chip price tables:

switch
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

1051Chapter 39 .Control Structures and Exception Handling each (My web site)

Sunday, August 26th, 2007

1051Chapter 39 .Control Structures and Exception Handling each one. In the past, the way to accommodate this was with a series of if. . .else constructions. The more conditions you must test, the less efficient the processing is, because each condition must be tested. Moreover, the sequence of clauses and braces can get very confusing. In NN4+ and IE4+, a control structure in use by many languages comes to JavaScript. The implementation is similar to that of Java and C, using the switch and case keywords. The basic premise is that you can create any number of execution paths based on the value of some expression. At the beginning of the structure, you identify what that expression is and then, for each execution path, assign a label matching a particular value. The formal syntax for the switchstatement is switch (expression) { case label1: statements [break] case label2: statements [break] … [default: statements] } The expressionparameter of the switch statement can evaluate to any string or number value. Labels are not surrounded by quotes, even if the labels represent string values of the expression. Notice that the breakstatements are optional. A break statement forces the switch expression to bypass all other checks of succeeding labels against the expression value. Another option is the defaultstatement, which provides a catchall execution path when the expression value does not match any of the casestatement labels. If you d rather not have any execution take place with a non-matching expression value, omit the default part of the construction. To demonstrate the syntax of a working switch statement, Listing 39-6 provides the skeleton of a larger application of this control structure. The page contains two separate arrays of different product categories. Each product has its name and price stored in its respective array. A SELECT list displays the product names. After a user chooses a product, the script looks up the product name in the appropriate array and displays the price. The trick behind this application is the values assigned to each product in the select list. While the displayed text is the product name, the VALUE attribute of each

1050 Part IV . JavaScript Core Language Reference (My web site)

Saturday, August 25th, 2007

1050 Part IV . JavaScript Core Language Reference Listing 39-5 (continued) } } out.value += After looping, i = + i + , j = + j + n } function run2() { var out = document.forms[0].output out.value = Running WITH labeled breakn outerLoop: for (var i = 0; i <= range; i++) { out.value += Outer loop # + i + n innerLoop: for (var j = 0; j <= range; j++) { out.value += Inner loop # + j + n if (i == targetA && j == targetB) { out.value += **BREAKING OUT OF OUTER LOOP**n break outerLoop } } } out.value += After looping, i = + i + , j = + j + n }

Breaking Out of Nested Labeled Loops


Look in the Results field for traces of these button scripts:

Results:

The switch Statement NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility . . . In some circumstances, a binary true or false decision path is not enough to handle the processing in your script. An objectproperty or variablevalue may contain any one of several values, and a separate execution path is required for switch
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

1049Chapter 39 .Control Structures and Exception (Web site designers) Handling preceding

Saturday, August 25th, 2007

1049Chapter 39 .Control Structures and Exception Handling preceding a logical block of executing statements, such as an if. . .thenor loop construction. The formal syntax looks like the following: labelID: statements For a breakor continue statement to apply itself to a labeled group, the label is added as a kind of parameter to each statement, as in break labelID continue labelID To demonstrate how valuable this can be in the right situation, Listing 39-5 contains two versions of the same nested loop construction. The goal of each version is to loop through two different index variables until both values equal the target values set outside the loop. When those targets are met, the entire nested loop construction should break off and continue processing afterward. To help you visualize the processing that goes on during the execution of the loops, the scripts output intermediate and final results to a textarea. In the version without labels, when the targets are met, only the simple break statement is issued. This breaks the inner loop at that point, but the outer loop picks up on the next iteration. By the time the entire construction has ended, a lot of wasted processing has gone on. Moreover, the values of the counting variables max themselves out, because the loops execute in their entirety several times after the targets are met. But in the labeled version, the inner loop breaks out of the labeled outer loop as soon as the targets are met. Far fewer lines of code are executed, and the loop counting variables are equal to the targets, as desired. Experiment with Listing 39-5 by changing the break statements to continue statements. Then closely analyze the two results in the Results textarea to see how the two versions behave. Listing 39-5: Labeled Statements Breaking Out of Nested Labeled Loops Here are the properties of the current window:

For debugging purposes, you can revise the function slightly to display the results in an alert dialog box. Replace the
HTML tag with the n carriage return character for a nicely formatted display in the alert dialog box. You can call this function from anywhere in your script, passing both the object reference and a string to it to help you identify the object after the results appear in an alert dialog box. If the showProps() function looks familiar to you, it is because it closely resembles the property inspector routines of The Evaluator (see Chapter 13). In Chapter 45, you can see how to embed functionality of The Evaluator into a page under construction so that you can view property values while debugging your scripts. The with Statement NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility . . . A with statement enables you to preface any number of statements by advising JavaScript on precisely which object your scripts will be talking about, so that you don t have to use full, formal addresses to access properties or invoke methods of the same object. The formal syntax definition of the with statement is as follows: with (object) { statements } The object reference is a reference to any valid object currently in the browser s memory. An example of this appears in Chapter 35 s discussion of the Mathobject. By embracing several Math-encrusted statements inside a with construction, your scripts can call the properties and methods without having to make the object part of every reference to those properties and methods. with
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Web site - 1046 Part IV . JavaScript Core Language Reference

Thursday, August 23rd, 2007

1046 Part IV . JavaScript Core Language Reference JavaScript in NN4+ and IE4+ brings you one more looping construction, called the do-while loop. The formal syntax for this construction is as follows: do { statements } while (condition) An important difference distinguishes the do-while loop from the whileloop. In the do-while loop, the statements in the construction always execute at least one time before the condition can be tested; in a whileloop, the statements may never execute if the condition tested at the outset evaluates to false. Use a do-while loop when you know for certain that the looped statements are free to run at least one time. If the condition may not be met the first time, use the while loop. For many instances, the two constructions are interchangeable, although only the while loop is compatible with all scriptable browsers. Looping through Properties (for-in) NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5 Compatibility . . . JavaScript includes a variation of the for loop, called a for-inloop, which has special powers of extracting the names and values of any object property currently in the browser s memory. The syntax looks like this: for (var in object) { statements } The object parameter is not the string name of an object but a reference to the object itself. JavaScript delivers an object reference if you provide the name of the object as an unquoted string, such as window or document. Using the varvariable, you can create a script that extracts and displays the range of properties for any given object. Listing 39-4 shows a page containing a utility function that you can insert into your HTML documents during the authoring and debugging stages of designing a JavaScript-enhanced page. In the example, the current windowobject is examined and its properties are presented in the page. Listing 39-4: Property Inspector Function