JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Window.prompt() Method

Name

Window.prompt() Method---get string input in a dialog

Availability

Navigator 2.0, Internet Explorer 3.0

Synopsis

window.prompt(message)
window.prompt(message, default)

Arguments

message

The plain text (not HTML) string to be displayed in the dialog. It should ask the user to enter the information you want. On Windows platforms, only one line messages are allowed.

default

An optionally specified string or integer which will be displayed as the default input in the dialog. If null is specified, or if this argument is not passed at all, then prompt() displays the string "<undefined>" as the default value. Pass the empty string ("") to make prompt() display an empty input box.

Returns

The string entered by the user, or the empty string if the user did not enter a string, or null if the user clicked Cancel.

Description

The prompt() method displays the specified message in a dialog box that also contains a textual input field and OK, Clear, and Cancel buttons. Platform-dependent graphics in the dialog help to indicate to the user that her input is desired.

The default argument specifies the text that initially appears in the input field. If default is null, or if this optional argument is not passed at all, then the initial text is "<undefined>". If default is the empty string ("") then the input field is initially empty. Otherwise, the input field displays the string or number that is passed as the default.

If the user clicks the Cancel button, prompt() returns null. If the user clicks the Clear button, prompt() erases any current text in the input field. If the user clicks the OK button, prompt() returns the value current value displayed in the input field. Note, however, that if the input field is displaying "<undefined>" because the default argument was null or unspecified, then prompt() returns the empty string and not the string "<undefined>". Also note that prompt() always returns the user's input as a string value, even if default was specified as a numeric value. If necessary, you can use parseInt() or parseFloat() to convert the returned string to a number.

The dialog box that is displayed by the prompt() method is modal--that is, it blocks all user input to the main browser window until the user dismisses the dialog by clicking on the OK or Cancel buttons. Since this method returns a value depending on the user's response to the dialog, JavaScript execution pauses in the call to prompt(), and subsequent statements are not executed until the user responds to the dialog.

Usage

Note that the question displayed in the dialog is a string of plain text, not of formatted HTML. Unlike the message displayed by the alert() and confirm() methods, the message displayed by prompt() may only be a single line long on Windows platforms.

See Also

"Window", "Window.alert()", "Window.confirm()"


Previous Home Next
Window.parent Book Index Window.scroll()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell