HTML and CSS Reference
In-Depth Information
When an
alert dialog box
is being displayed, the execution of the program code halts
until the user clicks the
OK
button. The alert message is limited to plain text and cannot
include HTML-formatted text. For more elaborate alert dialog boxes, you'd have to use
JavaScript to construct the appearance and content of a custom dialog box yourself; that
topic is beyond the scope of this tutorial.
Alert dialog boxes require no input from users other than to click the
OK
button to
continue the execution of the program code. The
confirm()
method
You can add a line return
to the dialog box message
text by including the
character code
\n
within
the text string.
confirm(
message
)
displays a
confirm dialog box
that prompts users for a
yes
or
no
response, where
message
is the text of the message that users must confirm or reject. For example, the
expression
confirm(“Load a new puzzle?”)
would display the dialog box shown in Figure 13-30.
Figure 13-30
browser window confirm dialog box
A confirm dialog box returns the result of a user's action as a Boolean value. If the user
clicks the
OK
button, a value of
true
is returned; otherwise, a value of
false
is returned.
For example, the user determines the Boolean value of the
showProfile
variable in the fol-
lowing command based on the button he or she clicks in the confirm dialog box:
var showProfile = confirm(“Show user profile?”);
If you need a text string returned instead of a Boolean value, you can display a
prompt
dialog box
by using the method
prompt(
message
,
default
)
where
message
is the text of the prompt message, and
default
is the default text string
returned by the dialog box if the
OK
button is clicked. The statement
var userName = prompt(“User Name”, “Enter your name”)
would display the dialog box shown in Figure 13-31, with the text entered by the user
saved to the
userName
variable. If the user clicked the
Cancel
button, no value would be
stored in the
userName
variable.
Figure 13-31
browser window prompt dialog box
Note that both the
confirm()
and
prompt()
methods also halt the actions of the
program until the user clicks either the
OK
or
Cancel
button.