LESSON 07 – ASSORTED TOPICS

 

THE ALERT FUNCTION

 

You can use the built-in alert function to make a small window pop-up and display a message.  The user hits ok to make the window disappear.

 

Example 1

 

The following code displays a message using a script section in the body.  So the message window appears immediately after the webpage is loaded.

 

<html>

<body>

 

<script type="text/javascript">

alert("Hey there buddy!");

</script>

 

This is the normal webpage part.

 

</body>

</html>

 

Click here to see this in action.

 

Example 2

 

The following code displays the message window only when a button is clicked.

 

<html>

<head>

<script type="text/javascript">

function sayHi()

{

   alert("Hey there buddy!");

}

</script>

 

</head>

<body>

 

This is the normal webpage part.

 

<form>

<input type="button" value="Click me" onClick="sayHi()">

</form>

 

</body>

</html>

 

Click here to see this in action.

 

COMBINING CONDITIONS

 

AND

 

We sometimes need to combine conditions inside one if statement and make sure that both are true.  We do so using the AND operator.  In javascript, the AND operator is &&. 

 

The syntax for this is:

 

if (condition1 && condition2)    //you say: if condition1 and condition2 are true

{

    //execute this if both conditions are true

}

 

OR

 

Sometimes, we need to check if one condition or the other is true.  To do this, we use the OR operator.  In javascript, the OR operator is ||.

 

The syntax for this is:

 

if (condition1 || condition2)    //you say: if condition1 or condition2 is true

{

    //execute this if one condition is true (or both conditions are true)

}