The if-then statement
if (Ret == "castle") {
location = 'ch03_1.html';
} else {
alert("Please try again")
}
The second line of the function is an if-then
statement. It tells the computer that if the variable Ret
equals "castle"
then change the URL location to ch03_1.html
. Otherwise, show the alert box
which says "Please try again."
Here is the format of an if-then
statement:
IF (a comparison) {
sequence if the comparison is true
} ELSE {
sequence is the comparison is false
}
For example, let's say you've just had the reader complete a form which included their age. You want all Senior Citizens to get one message, and everyone else to get another when they submit the form.
age=form.age.value | transfer the contents of the age box on the form to a variable called age. |
---|---|
if (age>=65) | The if statement begins with the question in parentheses. |
{ alert("Your form has been submitted. Ask about our Senior Discounts") } | The alert box will be displayed if the question is true, age IS greater or equal to 65 |
ELSE { alert("Your form has been submitted.") } | The alert box command following the word ELSE will only be displayed if the question is false, and age IS NOT greater or equal to 65. |