RESPOND WITH ALERTS

Alerts are one way to respond to a user's input. For example, in the first name prompt example in the previous section, you can add an alert to display the user's name and a greeting, such as the following:

var fname = prompt 
("Please enter your first name: ","");
alert ("Hello, " + fname);

This displays the alert box shown in Figure 3-3. The string is in quotation marks ("Hello, "), but the variable name (fname) is used without quotes. This tells the browser to use the value contained in the variable, not the variable name.

An Image
Figure 3-3: Displaying user input in an alert box.

If you want to display the variable name, you could add quotes to the variable name so that it's treated as a string:

alert ("Hello, " + "fname");

As you may have noticed, the alert box is modal, similar to the prompt box. The user must click OK for page processing to continue.

Use alerts sparingly. It can be very irritating to the user to be forced to respond to multiple alerts.