Window: prompt() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

window.prompt() instructs the browser to display a dialog with an optional message prompting the user to input some text, and to wait until the user either submits the text or cancels the dialog.

Under some conditions (when the user switches tabs, for example) the browser may not display a dialog, or may not wait for the user to submit text or to cancel the dialog.

Syntax

js
prompt()
prompt(message)
prompt(message, defaultValue)

Parameters

message Optional

A string of text to display to the user. Can be omitted if there is nothing to show in the prompt window.

defaultValue Optional

A string containing the default value displayed in the text input field.

Return value

A string containing the text entered by the user, or null.

Examples

Using a prompt with a message and default value

The following example shows how to check the returned value of a prompt. 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.

html
<button id="signButton">Check star sign</button>
<pre id="log"></pre>
js
const signButton = document.querySelector("#signButton");
const log = document.querySelector("#log");

signButton.addEventListener("click", () => {
  let sign = prompt("What's your sign?");

  if (sign === null) {
    log.innerText = "OK, maybe next time.";
  } else if (sign.toLowerCase() === "") {
    log.innerText = "Don't be shy, enter your sign!";
  } else if (sign.toLowerCase() === "scorpio") {
    log.innerText = "Wow! I'm a Scorpio too!";
  } else {
    log.innerText = `${sign} is my favorite!`;
  }
});

Prompt messages and default values

There are multiple ways to use a prompt, using prompt, window.prompt, and providing a message and default values:

js
// open a blank prompt window
sign = prompt();
// open a blank prompt window
sign = window.prompt();
// open a prompt with the text "Are you feeling lucky"
sign = window.prompt("Are you feeling lucky");
// open a prompt with the text "Are you feeling lucky" and "sure" as the default value
sign = prompt("Are you feeling lucky", "sure");

Notes

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 a modal window. Alternatively, a <dialog> element can be used for confirmations.

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 result is a string, which means you should sometimes cast the value given by the user. For example, if their answer should be a Number, you should cast the value to Number.

js
const number = Number(window.prompt("Type a number", ""));

Specifications

Specification
HTML
# dom-prompt-dev

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
prompt
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote Starting with Chrome 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote
footnote Firefox strips newline characters from the prompt response; see bug 1716229.
Opera – Full support
Opera 3 (Release date: 1997-12-01)
footnote
footnote Starting with Opera 33, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote Starting with Chrome Android 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote Firefox for Android strips newline characters from the prompt response; see bug 1716229.
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote
footnote Starting with Opera 33, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote Starting with Samsung Internet 5.0, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote
footnote Starting with WebView Android 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Deno – Full support
Deno 1.5 (Release date: 2020-10-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also