W3cubDocs

/Web APIs

Window: prompt() method

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 — for example, when the user switches tabs — the browser may not actually 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

js

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 macOS):

prompt() dialog in Chrome on macOS

Notes

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.

Please note that result is a string. That 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 aNumber = Number(window.prompt("Type a number", ""));

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).

Alternatively <dialog> element can be used to take user inputs.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
prompt
1Starting with Chrome 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
12
1Firefox strips newline characters from the prompt response; see bug 1716229.
4This function has no effect in the Modern UI/Metro version of Internet Explorer for Windows 8. It does not display a prompt to the user, and always returns undefined. It is not clear whether this is a bug or intended behavior. Desktop versions of IE do implement this function.
3Starting with Opera 33, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
1
4.4Starting with Chrome 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
18Starting with Chrome 46, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
4Firefox strips newline characters from the prompt response; see bug 1716229.
10.1Starting with Opera 33, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.
1
1.0Starting with Samsung Internet 5.0, this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modals.

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt