|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Using JavaScript StatementsThis section provides a quick reference to some of the more important JavaScript statements. Those listed here are in alphabetical ordermany have examples. The formatting of these entries means the following:
The break Statement The break statement terminates the current while or for loop and transfers program control to the statement that follows the terminated loop. Syntax break Example The following function scans the list of URLs in the current document and stops when it has seen all URLs or when it finds a URL that matches the input parameter searchName: function findURL(searchName) { var i = 0; for (i=0; i < document.links.length; i++) { if (document.links[i] == searchName) { document.writeln(document.links[i] + <br>) break; } } } The continue Statement The continue statement stops executing the statements in a while or for loop and skips to the next iteration of the loop. It doesnt stop the loop altogether like the break statement; instead, in a while loop, it jumps back to the condition. In a for loop, it jumps to the update expression. Syntax continue Example The following function prints the odd numbers between 1 and x; it has a continue statement that goes to the next iteration when i is even: function printOddNumbers(x) { var i = 0 while (i < x) { i++; if ((i % 2) == 0) // the % operator divides & returns the remainder continue else document.write(i, \n) } } The for Loop A for loop consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop. These parts do the following:
Syntax for ([initial_expr;] [condition;] [update_expr]) { statements } Example This simple for statement prints out the numerals from 0 to 9. It starts by declaring a loop counter variable, i, and initializing it to 0. As long as i is less than 9, the update expression increments i, and the statements in the loop body are executed. for (var i = 0; i <= 9; i++) { document.write(i); } The for…in Loop The for…in loop is a special form of the for loop that iterates the variable variable-name over all the properties of the object named object-name. For each distinct property, it executes the statements in the loop body. Syntax for (var in obj) { statements } Example The following function takes as its arguments an object and the objects name. It then uses the for…in loop to iterate through all the objects properties and writes them into the current Web page. function dump_props(obj,obj_name) { for (i in obj) document.writeln(obj_name + . + i + = + obj[i] + <br>); } The function Statement The function statement declares a JavaScript function; the function may optionally accept one or more parameters. To return a value, the function must have a return statement that specifies the value to return. All parameters are passed to functions by valuethe function gets the value of the parameter but cannot change the original value in the caller. Syntax function name([param] [, param] […, param]) { statements } Example This example defines a function called PageNameMatches, which returns TRUE if the string argument passed to the function is the title of the current document. function PageNameMatches(theString) { return (document.title == theString) } The if…else Statement The if…else statement is a conditional statement that executes the statements in block1 if condition is TRUE. In the optional else clause, it executes the statements in block2 if condition is FALSE. The blocks of statements can contain any JavaScript statements, including further nested if statements. Syntax if (condition) { statements } [else { statements }] Example This if...else statement calls the Message.Decrypt() method if the Message.IsEncrypted() method returns TRUE and calls the Message.Display() method otherwise. if (Message.IsEncrypted()) { Message.Decrypt(SecretKey); } else { Message.Display(); }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |