Window confirm()

Examples

Display a confirmation box:

confirm("Press a button!");
Try it Yourself »

Confirmation box with line-breaks:

confirm("Press a button!\nEither OK or Cancel.");
Try it Yourself »

More examples below.


Description

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button.

The confirm() method returns true if the user clicked "OK", otherwise false.


Note

A confirm box is often used if you want the user to verify or accept something.

A confirm box takes the focus away from the current window, and forces the user to read the message.

Do not overuse this method. It prevents the user from accessing other parts of the page until the box is closed.

See Also:

The alert() Method

The prompt() Method

Syntax

confirm(message)

Parameters

Parameter Description
message Optional.
The text to display in the confirm box.

Return Value

Type Description
A booleantrue if the user clicked OK, otherwise false.


More Examples

Display a confirmation box, and output what the user clicked:

let text;
if (confirm("Press a button!") == true) {
  text = "You pressed OK!";
} else {
  text = "You canceled!";
}
Try it Yourself »

Browser Support

confirm() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.