break | NN 2 IE 3 ECMA 1 |
break [label]
See the label statement.
catch | NN 2 IE 3 ECMA 1 |
continue | NN 2 IE 3 ECMA 1 |
continue [label]
outerLoop: for (var i = 0; i <= maxValue1; i++) { for (var j = 0; j <= maxValue2; j++) { if (j*i == magic2) { continue outerLoop; } } }
do/while | NN 4 IE 4 ECMA 3 |
do { statements } while (condition)
var i = 1; do { window.status = "Loop number " + i++; } while (i <= 10) window.status = "";
for | NN 2 IE 3 ECMA 1 |
for ([initExpression]; [condition]; [updateExpression]) { statements }
var userEntry = document.forms[0].entry.value; var oneChar; for (var i = 0; i < userEntry.length; i++) { oneChar = userEntry.charAt(i); if (oneChar < "0" || oneChar > "9") { alert("The entry must be numerals only."); } }
for/in | NN 2 IE 3 ECMA 1 |
for (varName in objectRef) { statements }
function showProps( ) { objName = "image"; obj = document.images[0]; var msg = ""; for (var i in obj) { msg += objName + "." + i + "=" + obj[i] + "\n"; } alert(msg); }
if | NN 2 IE 3 ECMA 1 |
if (condition) { statement(s) if true }
if (myDateObj.getMonth( ) == 1) { calcMonthLength( ); }
if/else | NN 2 IE 3 ECMA 1 |
if (condition) { statement(s) if true } else { statement(s) if false }
var theMonth = myDateObj.getMonth( ); if (theMonth == 1) { monLength = calcLeapMonthLength( ); } else { monLength = calcMonthLength(theMonth); }
label | NN 4 IE 4 ECMA 3 |
labelName:
outerLoop: for (var i = 0; i <= maxValue1; i++) { for (var j = 0; j <= maxValue2; j++) { if (i == magic1 && j == magic2) { break outerLoop; } } }
return | NN 2 IE 3 ECMA 1 |
return [value]
function validateNumber(form) { var oneChar; for (var i = 0; i < userEntry.length; i++) { oneChar = form.entry.value.charAt(i); if (oneChar < "0" || oneChar > "9") { return false; } } return true; }
switch/case | NN 4 IE 4 ECMA 3 |
switch (expression) { case label1: statements [break;] case label2: statements [break;] ... [default: statements] }
var productList = document.forms[0].prodList; var chosenItem = productList.options[productList.selectedIndex].value; switch(chosenItem) { case "Small Widget": document.forms[0].price.value = "44.95"; break; case "Medium Widget": document.forms[0].price.value = "54.95"; break; case "Large Widget": document.forms[0].price.value = "64.95"; break; default: document.forms[0].price.value = "Nothing Selected"; }
throw | NN 6 IE 5 ECMA 3 |
throw value;
function processNumber(inputField) { try { var inpVal = parseInt(inputField.value, 10); if (isNaN(inpVal)) { var msg = "Please enter a number only."; var err = new Error(msg); if (!err.message) { err.message = msg; } throw err; } // process number } catch (e) { alert(e.message); inputField.focus( ); inputField.select( ); } }
try/catch | NN 6 IE 5 ECMA 3 |
You can use try/catch constructions only in browsers that support them. To protect older browsers from seeing this construction, place all affected code inside a <script> tag that explicitly requires JavaScript 1.5 or later (with the language = "JavaScript1.5" attribute.
try { statement(s) that could cause error } catch (errorInfo) { process error(s) gracefully }
function insertOneNode(baseNode, newNode, position) { try { baseNode.insertBefore(newNode, baseNode.childNodes[position]); } catch (e) { // handle W3C DOM Exception types switch (e.name) { case "HIERARCHY_REQUEST_ERR" : // process bad tree hierarchy reference break; case "NOT_FOUND_ERR" : // process bad refNode reference break; default : // process all other exceptions } } return true; }
while | NN 2 IE 3 ECMA 1 |
while (condition) { statements }
var i = 0; while (!document.forms[0].radioGroup[i].checked) { i++; } alert("You selected item number " + (i+1) + ".");
with | NN 2 IE 3 ECMA 1 |
with (objectRef) { statements }
with (document.forms[0]) { name1 = firstName.value; name2 = lastName.value; mail = eMail.value; }
Copyright © 2003 O'Reilly & Associates. All rights reserved.