Display a prompt box which ask the user for her/his name, and output a message:
More "Try it Yourself" examples below.
The prompt() method displays a dialog box that prompts the visitor for input.
A prompt box is often used if you want the user to input a value before entering a page.
Note: When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. Do not overuse this method, as it prevent the user from accessing other parts of the page until the box is closed.
The prompt() method returns the input value if the user clicks "OK". If the user clicks "cancel" the method returns null.
Method | |||||
---|---|---|---|---|---|
prompt() | Yes | Yes | Yes | Yes | Yes |
Parameter | Type | Description |
---|---|---|
text | String | Required. The text to display in the dialog box |
defaultText | String | Optional. The default input text |
Return Value: | A String. If the user clicks "OK", the input value is returned. If the user clicks "cancel", null is returned. If the user clicks OK without entering any text, an empty string is returned. |
---|
Using the switch statement together with prompt() to execute a block of code based on user input:
Window Object: alert() Method
Window Object: confirm() Method