Window.confirm() (Method)

Present a confirmation dialog box.


JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Opera - 3.0
Property/method value type:Boolean primitive
JavaScript syntax:-myResult = confirm(aString)
-myResult = myWindow.confirm(aString)
Argument list:aStringSome text to explain what is to be confirmed

This presents a modal dialog containing the confirmation message and two buttons, OK and Cancel. This is useful because you often need confirmation from a user.

This method is useful for debugging. An example showing how it can be used for debugging a recursive function is given below.

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

The title bar of the dialog box cannot be changed from its default setting which 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 confirm() dialog box is modal and blocking. The script must wait for a response from the user.

When the user clicks on either of the buttons, the result returned by the method indicates which one was chosen. The true value indicates the OK button was clicked and false indicates the Cancel button.

Example code:

   // Example showing the use of a confirm() dialog to
   // debug recursive calls provided by Martin Honnen.
   function showTime()
      if (confirm('Time is: ' + new Date() + '. Show again?'))
         setTimeout('showTime()', 1000 * 5);

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



Wrox Instant JavaScript - page - 78