Main Page

System dialogs

System dialogs
Aside from popping up new browser windows, several other methods
pop up
information to the user
utilizing methods of the
, and
You are already familiar with the syntax of a call to
because it has been used in a large number
of examples up to this point. This method accepts one argument, which is the text to display to the user.
is called, the browser creates a system message box that displays the given text with an
OK button. For example, the following line of code causes the message box in Figure 5-6 to be displayed:
alert(“Hello world! “);
Figure 5-6
Alert dialogs are typically used when users must be made aware of something that they have no control
over, such as errors. Often alert dialogs are displayed when the user has entered invalid data into a form.
The second type of dialog is displayed by calling
. A confirm dialog looks similar to an alert
dialog in that it displays a message to the user. The main difference between the two is the presence of
a Cancel button along with the OK button in the confirm dialog, which allows the user to indicate if a
given action should be taken. For example, the following line of code displays the confirm dialog shown
in Figure 5-7:
confirm(“Are you sure? “);
Figure 5-7
Opening new windows can be helpful to users in some instances, but generally
speaking it’s better to keep pop-up windows to a minimum. A large industry has
popped up (no pun intended) selling pop-up ads on Web sites, which most users
find incredibly annoying. To this end, many users have installed pop-up blockers
that automatically block all pop-up windows unless the user specifically allows
them. Remember, pop-up blockers don’t know the difference between a legitimate
pop-up window and an advertisement, so it’s always best to warn a user when a
window is going to be popped up.
JavaScript in the Browser
08_579088 ch05.qxd 3/28/05 11:37 AM Page 143

JavaScript EditorFree JavaScript Editor     Ajax Editor