Friday, September 5, 2008

Alert boxes

An alert box is a small window that pops up with a yellow warning sign and a (usually) important message.
With JavaScript we can either show a message to the user either before a page loads or in response to a user
action. Here we will make an alert box that appears before the page loads. It will look like this:


Exercise

1. In Notepad, open base.html, your starter HTML page template.
2. Now, within the tags, insert the following code:

<script language="JavaScript">
<!--
alert("This is a class on JavaScript");
//-->


This makes a pop-up box which says "This is a class on JavaScript" with an OK button that allows the user to close it and continue. The message can be changed to whatever you like. This type of pop-up box is called an alert box because it can only be used to alert a viewer. It cannot be used to control where the user may go. alert() is a method that takes care of displaying the box. the line "This is a class on JavaScript" is a parameter for the alert() method. Some more examples of methods are:

confirm("put message here"); This is a next type of box we'll be looking at.
prompt("put request here"); This asks the user to enter some text.

You probably noticed at the end of a method, after the right parenthesis, there is a semi-colon. In JavaScript, a semi-colon is used to end a "statement".

Now we will make another type of pop-up box.

Confirm Boxes