The Window.prompt()
displays a dialog with an optional message prompting the user to input some text.
result = window.prompt(message, default);
message
Optional
default
Optional
"undefined"
is the default value.A string containing the text entered by the user, or null
.
let sign = prompt("What's your sign?"); if (sign.toLowerCase() == "scorpio") { alert("Wow! I'm a Scorpio too!"); } // there are many ways to use the prompt feature sign = window.prompt(); // open the blank prompt window sign = prompt(); // open the blank prompt window sign = window.prompt('Are you feeling lucky'); // open the window with Text "Are you feeling lucky" sign = window.prompt('Are you feeling lucky', 'sure'); // open the window with Text "Are you feeling lucky" and default value "sure"
When the user clicks the OK button, text entered in the input field is returned. If the user clicks OK without entering any text, an empty string is returned. If the user clicks the Cancel button, this function returns null
.
The above prompt appears as follows (in Chrome on OS X):
A prompt dialog contains a single-line textbox, a Cancel button, and an OK button, and returns the (possibly empty) text the user entered into that textbox.
The following text is shared between this article, DOM:window.confirm and DOM:window.alert Dialog boxes are modal windows; they prevent the user from accessing the rest of the program's interface until the dialog box is closed. For this reason, you should not overuse any function that creates a dialog box (or modal window).
Please note that result is a string. That means you should sometimes cast the value given by the user. For example, if his answer should be a Number, you should cast the value to Number.
const aNumber = Number(window.prompt("Type a number", ""));
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'prompt()' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes
|
? | Yes | Yes
|
? | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | ? | Yes | ? | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/window/prompt