Make your own free website on

How to make it even more interactive

When you first entered this page you were asked a couple of questions. You will get different pop-ups depending on your answers to the questions. My name is Matt reload the page and use that as your name. Make your age really large or small (over 100 or under 5) and you will get a different response. I'm 27 and the pop-ups will vary depending on your age in relation to mine. Reload the page to try different values and see what happens.

You've already learned how to use variable on the last page. Now I'll teach you some way to do different things depending on what the person inputs to prompts.

Here is a new addition to add to your script.

if(name == "Matt") { alert("Hey that's my name");} This part of the script will check and see if the name entered equals Matt. There must be two equal signs or the browser will store Matt in the variable name instead of just comparing.
If the results of the comparison turn out to be true it will carry out the statements between the { and } signs. After each statement there must be a semicolon ; just like the other commands in a script.

Now here is a complete script that check to see if the name is a certain one.

<!-- Hide Script
var name = prompt("What is your name","");
if(name == "Matt"){ alert("Hey, that's my name");}
alert("Welcome to my page "+name);
//-- end script -->
</script> Now that script will check to see if the name is Matt. If it is it will give the alert Hey, that's my name. Then no matter what the name it will give the welcome alert. So if someone enters the name Matt they will get both alerts where as someone not named Matt will only get one pop-up.

You could also have multiple commands following the if statement that would be carried out if the variable equaled a certain response as follows.

if(var name == "Matt"){ alert("Hey that's my name");
alert("I wonder if you are me?");}

Now let's do some variable comparing with numbers like I did in the age question.

if(age == 27){alert("Hey that's my age");} That's a simple on just like the one that compares the name.
Here's the statement to check if it larger or less than. if((age > 100) || (age < 5)){ alert("Something tells me you're lying!");} This tests to see if age is large than 100 or smaller than 5. if either one of these things are true it will do the pop-up. If neither one is true it will skip it.
If you want two things to be true before carrying out the commands use this statement.
if((age == 27) && (name == "Matt"))
{alert("Hey that's my name and my age. You must be me");
var check = prompt("Are you me?");
if (check == "Yes") { alert("I thought you were me);}
else { alert("You sure are alot like me");}}

In order to check for 3 thing you would write it like this.

if((age == 27) && (name == "Matt")){
if(question == "answer"){
alert("Your alert for this");}}

When writing complex ones like this or even more complex keep track of how many { you have and make sure you end it with the right amount. This is a commen error I make, not closing my brackets correctly.

Take lesson two and see if you can write your own. A sample script is provided wich you can cut and paste into the editing window. Try changing the questions and arrangements of the alerts that follow.

Lesson II

On the following page you will learn how to write your comments directly to the page instead of using alerts

Writing directly to page

|| index||page 1||page 2||page 3||page 4||page 5||page 6||page 7||Page 8||