Main Page

status bar

To determine if the user clicked OK or Cancel, the
method returns a Boolean value:
OK was clicked,
if Cancel was clicked. Typical usage of a confirm dialog usually looks like this:
if (confirm(“Are you sure? “)) {
alert(“I’m so glad you’re sure! “);
} else {
alert(“I’m sorry to hear you’re not sure. “);
In this example, the confirm dialog is displayed to the user in the first line, which is a condition of the
statement. If the user clicks OK, an alert is displayed saying, “I’m so glad you’re sure!” If, however, the
Cancel button is clicked, an alert is displayed saying, “I’m sorry to hear you’re not sure.” This type of
construct is often used when the user tries to delete something, such as an e-mail in his or her inbox.
The final dialog is displayed by calling
, and as you might expect, this dialog prompts for
input from the user. Along with OK and Cancel buttons, this dialog also has a text box where the user is
asked to enter some data. The
method accepts two arguments: the text to display to the user
and the default value for the text box (which can be an empty string if you so desire). The following line
results in the window displayed in Figure 5-8 being shown:
prompt(“What’s your name? “, “Michael”);
Figure 5-8
The value in the text box is returned as the function value if the OK button is clicked; if the Cancel but-
ton is clicked,
is returned. The
method is often used like this:
var sResult = prompt(“What is your name? “, “”);
if (sResult != null) {
alert(“Welcome, “ + sResult);
I have a few final points to cover regarding these three dialogs. First, all the dialog windows are system
windows, meaning that they may appear different on different operating systems (and sometimes, on
different browsers). This also means that you have no control over the display of the window in terms of
fonts, colors, and so on.
Second, the dialogs are all
, meaning that the user cannot do anything else in the browser until the
dialog is dismissed by clicking the OK button or Cancel buttons. This is a common method of controlling
user behavior to ensure that important information is delivered in a secure way.
The status bar
The status bar is the area in the bottom border that displays information to the user (see Figure 5-9).
Chapter 5
08_579088 ch05.qxd 3/28/05 11:37 AM Page 144

JavaScript EditorFree JavaScript Editor     Ajax Editor