The if-then statement.
OK, once again, here is the entire code. Give it a look, then we'll break it down. This is a good example of an If-Then statement. Password scripts can also be combined with an encryption function so that
hackers can't break in simply by viewing your source code. The purpose of this chapter, however, is to give you some practice with if-then statements.
--------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Chapter 3, If-then statements</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -
function password() {
Ret = prompt('Type the word castle',"");
if(Ret=="castle") {
location = 'ch03_1.htm';
} else {
alert("Please try again")
}
}
// - End of JavaScript - -->
</SCRIPT>
</HEAD>
<BODY>
<A HREF="javascript:password()">
<IMG SRC="pict1.gif" NAME="pic1" ALT="about us!" BORDER="0" align="left"></A>
<H3>Click the image to enter a password protected document. Try a wrong entry first.</H3>
</BODY>
</HTML>
BREAK DOWN OF CODE
function password() {
Ret = prompt('Type the word castle',"");
if(Ret=="castle") {
location = 'ch03_1.html';
--------------------------------------------------------------------------------
The prompt
The first line of the function, password() is:
Ret = prompt('Type the word castle',"");
This command tells the computer to open a prompt box, and assign whatever the user types to the variable, Ret. Therefore if the user types "chickensoup", the computer will execute the command
Ret="chickensoup"
I can then compare the variable Ret with other text to determine if the user typed in the correct password.
The parentheses contain the message inside the prompt box, then the default text. Look at this sample command, then click the show me button
prompt("Please type your name","Newt")
If you do not want a default, simply place 2 quotes after the comma like I did in the original example
if(Ret=="castle") {
location = 'ch03_1.html';
} else {
alert("Please try again")
}
--------------------------------------------------------------------------------
The if-then statement
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.