Window.alert

  • Revision slug: Web/API/window.alert
  • Revision title: window.alert
  • Revision id: 454259
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef("Window") }}

The Window.alert() method displays an alert dialog with the optional specified content and an OK button.

Syntax

window.alert(message);
  • message is an optional string of text you want to display in the alert dialog, or, alternatively, an object that is converted into a string and displayed.

Example

window.alert("Hello world!");

produces:

Image:AlertHelloWorld.png

Notes

The alert dialog should be used for messages which do not require any response on the part of the user, other than the acknowledgement of the message.

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

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

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

Specification

{{ DOM0() }}

See also

  • {{domxref("window.confirm","confirm")}}
  • {{domxref("window.prompt","prompt")}}
  • For Mozilla Chrome see {{ifmethod("nsIPromptService","alert")}} and {{ifmethod("nsIPromptService","alertCheck")}}

Revision Source

<p>{{ DomRef("Window") }}</p>
<p>The <strong><code>Window.alert()</code></strong> method displays an alert dialog with the optional specified content and an OK button.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
window.alert(<em>message</em>);</pre>
<ul>
  <li><code>message</code> is an optional string of text you want to display in the alert dialog, or, alternatively, an object that is converted into a string and displayed.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush: js">
window.alert("Hello world!");
</pre>
<p>produces:</p>
<p><img alt="Image:AlertHelloWorld.png" src="/files/130/AlertHelloWorld.png" /></p>
<h2 id="Notes" name="Notes">Notes</h2>
<p>The alert dialog should be used for messages which do not require any response on the part of the user, other than the acknowledgement of the message.</p>
<p><span class="comment">The following text is shared between this article, DOM:window.prompt and DOM:window.confirm</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>Chrome users (e.g. extensions) should use methods of {{interface("nsIPromptService")}} instead.</p>
<p>{{firefox_min_version("23.0")}} The argument is now optional like required by the spec.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<p>{{ DOM0() }}</p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{domxref("window.confirm","confirm")}}</li>
  <li>{{domxref("window.prompt","prompt")}}</li>
  <li>For <a href="/en-US/docs/Chrome" title="/en-US/docs/Chrome">Mozilla Chrome</a> see {{ifmethod("nsIPromptService","alert")}} and {{ifmethod("nsIPromptService","alertCheck")}}</li>
</ul>
Revert to this revision