English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
prompt()method displays a dialog box with an optional message, prompting the user to enter some text.
The prompt dialog contains a single-line text box, a 'Cancel' button, and an 'OK' button.
If the user clicks 'OK', the prompt() method will return the input value. If the user clicks 'Cancel', the method returnsnull.
If you want the user to enter a value before entering the page, a prompt box is usually used.
Dialog is a modal window-Before the dialog is closed, they prevent the user from accessing the rest of the program interface. Therefore, you should not overuse any functions that create dialogs (or modal windows).
window.prompt(msg, default)
var name = prompt("Enter your name", "Someone"); if (name != null) { document.getElementById("output").innerHTML = "Hello " + name; }Test See‹/›
All browsers fully support the prompt() method:
Method | |||||
prompt() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
msg | (Optional) String displayed in the prompt box |
default | (Optional) Default input text |
Return Value: | String containing user input text |
---|
This example demonstrates the different types of dialog boxes supported by JavaScript:
Click the button below to display different dialog boxes:
Window (Window) Reference:alert() method
Window (Window) Reference:confirm() method