JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference

Window.alert() Method


Window.alert() Method---display a message in a dialog box


Navigator 2.0, Internet Explorer 3.0





The plain text (not HTML) string to display in a dialog box popped up over window.




The alert() method displays the specified message to the user in a dialog box. The dialog box contains an OK button that the user can click to dismiss the dialog box.

The dialog box that is displayed is non-modal. That is, the user can continue to interact with the browser window while the dialog is displayed. JavaScript execution continues while the dialog is displayed; it does not pause until the user dismisses the dialog.


Perhaps the most common use of the alert() method is to display error messages when the user's input to some form element is invalid in some way. The alert dialog can inform the user of the problem and explain what needs to be corrected to avoid the problem in the future. The appearance of the alert() dialog box is platform-dependent, but generally it contains graphics that indicate that the message indicates an error, a warning, or an alert of some kind. While alert() can display any desired message, the "alert" graphics of the dialog mean that this method is not appropriate for simple informational messages like "Welcome to my home page" or "You are the 177th visitor this week!"

Note that the message displayed in the dialog is a string of plain text, not formatted HTML. You can use the newline character, \n, in your strings to break your message across multiple lines. You can also do some very rudimentary formatting using spaces and can approximate horizontal rules with underscore characters, but the results you achieve will depend greatly on the font used in the dialog, and will thus be system dependent.

In Netscape Navigator, the message displayed is prefaced with "JavaScript Alert:". The alert box in Microsoft Internet Explorer does not display any similar indication that the message comes from a JavaScript program; this has been raised as a possible security concern.

See Also

"Window", "Window.confirm()", "Window.prompt()"

Previous Home Next
Window Book Index Window.blur()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz