Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 901045 of Window.prompt()

  • Revision slug: Web/API/Window/prompt
  • Revision title: Window.prompt()
  • Revision id: 901045
  • Created:
  • Creator: jpmedley
  • Is current revision? No
  • Comment

Revision Content

{{ApiRef("Window")}}

The Window.prompt() displays a dialog with an optional message prompting the user to input some text.

Syntax

result = window.prompt(message, default);
  • result is a string containing the text entered by the user, or null.
  • message is a string of text to display to the user. This parameter is optional and can be omitted if there is nothing to show in the prompt window.
  • default is a string containing the default value displayed in the text input field. It is an optional parameter. Note that in Internet Explorer 7 and 8, if you do not provide this parameter, the string "undefined" is the default value.

Example

var sign = prompt("What's your sign?");

if (sign.toLowerCase() == "scorpio") {
  alert("Wow! I'm a Scorpio too!");
}

// there are many way to use prompt feature
var sign = window.prompt(); // open the blank prompt window
var sign = prompt();       //  open the blank prompt window
var sign = window.prompt('Are you feeling lucky'); // open the window with Text "Are you feeling lucky"
var 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):

prompt() dialog in Chrome on OS X

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.

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. var aNumber = Number(window.prompt("Type a number", "")); 

Mozilla Chrome users (e.g. Firefox extensions) should use methods of {{interface("nsIPromptService")}} instead.

Starting with Chrome {{CompatChrome(45.0)}} this method is blocked inside an {{htmlelement("iframe")}} unless it sandbox attribute has the value allow-modal and the the modal flag is enabled.

In Safari, if the user clicks Cancel, the function returns an empty string. Therefore, it doesn't differentiate canceling from the user entering an empty string in the textbox.

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

Specification

Specification Status Comment
{{SpecName('HTML5 Web application', '#dom-prompt', 'prompt()')}} {{Spec2('HTML5 Web application')}} Initial definition.

See also

  • {{domxref("window.alert", "alert")}}
  • {{domxref("window.confirm", "confirm")}}

Revision Source

<div>{{ApiRef("Window")}}</div>

<p>The <code>Window.prompt()</code> displays a dialog with an optional message prompting the user to input some text.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
<em>result</em> = window.prompt(<em>message</em>, <em>default</em>);
</pre>

<ul>
 <li><code>result</code> is a string containing the text entered by the user, or null.</li>
 <li><code>message</code> is a string of text to display to the user. This parameter is optional and can be omitted if there is nothing to show in the prompt window.</li>
 <li><code>default</code> is a string containing the default value displayed in the text input field. It is an optional parameter. Note that in Internet Explorer 7 and 8, if you do not provide this parameter, the string "undefined" is the default value.</li>
</ul>

<h2 id="Example" name="Example">Example</h2>

<pre class="brush:js">
var sign = prompt("What's your sign?");

if (sign.toLowerCase() == "scorpio") {
  alert("Wow! I'm a Scorpio too!");
}

// there are many way to use prompt feature
var sign = window.prompt(); // open the blank prompt window
var sign = prompt();       //  open the blank prompt window
var sign = window.prompt('Are you feeling lucky'); // open the window with Text "Are you feeling lucky"
var sign = window.prompt('Are you feeling lucky', 'sure'); // open the window with Text "Are you feeling lucky" and default value "sure"</pre>

<p>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 <code>null</code>.</p>

<p>The above prompt appears as follows (in Chrome on OS&nbsp;X):</p>

<p><a href="https://mdn.mozillademos.org/files/11303/prompt.png"><img alt="prompt() dialog in Chrome on OS X" src="https://mdn.mozillademos.org/files/11303/prompt.png" style="height:298px; width:535px" /></a></p>

<h2 id="Notes" name="Notes">Notes</h2>

<p>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.</p>

<p><span class="comment">The following text is shared between this article, DOM:window.confirm and DOM:window.alert</span> 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).</p>

<p>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.&nbsp;<span style="background-color:rgb(246, 246, 242); font-family:courier new,andale mono,monospace; font-size:12px; line-height:normal">var aNumber = Number(window.prompt("Type a number", ""));&nbsp;</span></p>

<p><a href="/en-US/Chrome" title="Chrome">Mozilla Chrome</a> users (e.g. Firefox&nbsp;extensions) should use methods of {{interface("nsIPromptService")}} instead.</p>

<p>Starting with Chrome {{CompatChrome(45.0)}} this method is blocked inside an&nbsp;{{htmlelement("iframe")}} unless it sandbox attribute has the value allow-modal and the&nbsp;the modal flag is enabled.</p>

<p>In Safari, if the user clicks Cancel, the function returns an empty string. Therefore, it doesn't differentiate canceling from the user entering an empty string in the textbox.</p>

<p>This 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 <code>undefined</code>. It is not clear whether this is a bug or intended behavior. Desktop versions of IE do&nbsp;implement this function.</p>

<h2 id="Specification" name="Specification">Specification</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 Web application', '#dom-prompt', 'prompt()')}}</td>
   <td>{{Spec2('HTML5 Web application')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li>{{domxref("window.alert", "alert")}}</li>
 <li>{{domxref("window.confirm", "confirm")}}</li>
</ul>
Revert to this revision