Window.alert() (Method)

Present an alerting dialog box.


JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Opera - 3.0
Property/method value type:undefined
JavaScript syntax:-alert(aString)
Argument list:aStringSome text to display in the alert box

This presents a dialog containing the warning message and an OK button.

Note that the text that is presented in the dialog is unformatted text and you cannot use HTML in the dialog box.

The title bar of the dialog box cannot be changed from its default setting that tells you that the dialog was invoked by JavaScript. In some browsers, it may just display the name of the browser. This is intended to stop script programmers from masquerading their dialog boxes as those of operating system diagnostics and login screens.

The alert() dialog box is modal on most platforms. It also blocks the script from continuing except on some versions of Netscape Navigator on Unix platforms. This behavior may become more common as desktop operating systems become more Unix-like and the Netscape Navigator core source code is deployed on newer operating systems. It is possible that this behavior will be exhibited on Mac OS X.

This method is useful for debugging. You can use it much like you would have used a printf() in C language debugging. Using alert() can sometimes be useful as an observable effect of calling a function or event handler. If you don't see the alert box, it's likely the event didn't call your handler.

You may be able to accomplish some rudimentary formatting but realistically due to font differences on platforms the only meaningful formatting is to place newline characters (\n) into the text to introduce a line break and to insert leading spaces for indentation.

This method does not return any meaningful value and if it is used in an assignment, the value undefined will be used.


See also:Debugging - client-side, Dialog boxes, Dialog object, Frame object, Window object, Window.confirm(), Window.prompt()

insert figure 0119


Wrox Instant JavaScript - page - 78