window.confirm

  • Revision slug: Web/API/Window.confirm
  • Revision title: window.confirm
  • Revision id: 502409
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Remove "Fixit" tagMoved From Web/API/window.confirm to Web/API/Window.confirm

Revision Content

{{DomRef("Window")}}

The Window.confirm() method displays a modal dialog with an optional message and two buttons, OK and Cancel.

Syntax

result = window.confirm(message);
  • message is the optional string to be displayed in the dialog.
  • result is a boolean value indicating whether OK or Cancel was selected (true means OK).

Example

if (window.confirm("Are you sure you wish to leave?")) { 
  window.open("exit.html", "Thanks for Visiting!", "");
}

produces:

An example of the javascript confirm dialog box prompt
 

Notes

The following text is shared between this article, DOM:window.prompt 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).

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

{{gecko_minversion_inline("23.0")}} The argument is now optional like required by the spec.

Specification

Specified in HTML5.

See also

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

Revision Source

<div>
 {{DomRef("Window")}}</div>
<p>The <code><strong>Window.confirm()</strong></code> method displays a modal dialog with an optional message and two buttons, OK and Cancel.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre>
<i>result</i> = window.confirm(<i>message</i>);
</pre>
<ul>
 <li><code>message</code> is the optional string to be displayed in the dialog.</li>
 <li><code>result</code> is a boolean value indicating whether OK or Cancel was selected (<code>true</code> means OK).</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre>
if (window.confirm("Are you sure you wish to leave?")) { 
  window.open("exit.html", "Thanks for Visiting!", "");
}
</pre>
<p>produces:</p>
<p><img alt="An example of the javascript confirm dialog box prompt" src="https://mdn.mozillademos.org/files/5971/Screen%20Shot%202013-08-22%20at%205.07.42%20PM.png" style="width: 420px; height: 154px;" /><br />
 &nbsp;</p>
<h2 id="Notes" name="Notes">Notes</h2>
<p><span class="comment">The following text is shared between this article, DOM:window.prompt 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><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>{{gecko_minversion_inline("23.0")}} The argument is now optional like required by the spec.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<p>Specified in HTML5.</p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{domxref("window.alert","alert")}}</li>
 <li>{{domxref("window.prompt","prompt")}}</li>
</ul>
Revert to this revision