Javascript: fifth lesson


Now let's see the most important Javascript statements:

  • The If...else statement
  • For and While statements
  • // and /*...*/ statements

If...else

If...else is used to perform some statements when a logical condition is true or it's false. This is its structure:

If (condition) { statements }
or:
If (condition) { statements } else { statements }

Example

Type 'house' here:then click outside of the field.

Ok, now try to type anything else and click outside of the field.

And this is the HTML page:

<HTML><HEAD><SCRIPT LANGUAGE="JavaScript1.2">
<!-- Hide script from old browser

function jokefunction(text1) {

if (text1=="house")

{alert("Yes, it's right!")}

else

{alert ("Ok! You typed a different word...)")}

}

// End the hiding here. -->
</SCRIPT></HEAD>

<BODY> Type 'house' here:

<input type="text" size="20" name="text1"onchange="Jokefunction(this.value)">

then click outside of the field.<br>

Ok, now try to type a different word and click outside of the field.

</BODY></HTML>

What's happen? Well, when you change something inside of the field, a Change event occurs. So the OnChange event-handler calls 'jokefunction', passing to her the value of the 'text1' field. Notice that you could write 'text1.value' instead of 'this.value'. This.something means the current object. In this case, 'this.' means the current field. Well, what's happen inside of jokefunction? You can check the value of that field, by comparing it with the 'house' string. If that condition is true, the browser execute the first statement, else it execute the second one.

For and While

For and While are loops. A loop is a set of statements which are executed until a specified condition becomes false. This is its structure:

for (initial-expression ; condition ; increment-expression ) {statements}

What's happen inside of a for loop?

  1. The initializing expression initial-expression, if any, is executed (this
    expression usually initializes one or more loop counters).

  2. The condition expression is evaluated. If it's true, the loop statements are executed, otherwise the for loop terminates.

  3. The expression increment-expression is executed (this expression usually increment one or more loop counters by one).

  4. The statements are executed, and control returns to step 2.

The While statement is similar to the for statement. This is its structure:

while (condition) {statements }

In this statement, statements are executed when the condition is true.

Example

And this is the HTML page:

<HTML><HEAD><SCRIPT LANGUAGE="JavaScript1.2">
<!-- Hide script from old browser

function myloop() {

for (ctr=1; ctr<=5; ctr++) {alert (ctr)}

}

// End the hiding here. -->
</SCRIPT></HEAD>

<BODY>

<input type="button onClick="myloop()" value="Click here!">

</BODY>

</HTML>

// and /*...*/

// is used for comments. If you type:

// this is a comment

That line is ignored. When there are more than one line of comment, you can use /* at the beginning of comment and */ at the end of it. So:

/*

This

is

a

comment

*/


Index           Home  Back       About  Contact us!

Copyright (c) 1998-2006 Wowarea