Javascript Reference - Window confirm() Method








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

Browser Support

confirm Yes Yes Yes Yes Yes

Syntax

confirm(message);

Parameter Values

Parameter Description
message Optional. Set the text to display in the confirm box. Required in Firefox.




Return Value

Type Description
Boolean A boolean value indicating whether 'OK' or 'Cancel' was clicked in the dialog box:
  • true - the user clicked 'OK'
  • false - the user clicked 'Cancel' or the 'x' (close) button.

Example

The following code shows how to display a confirmation box.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from  w  w  w. j  a  va  2 s  .  com-->
<script>
function myFunction() {
    confirm("Press a button!");
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to display a confirm box, and output what the user clicked.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from   ww  w  .  ja  v  a2 s .c  o  m-->
<p id="demo"></p>

<script>
function myFunction() {
    var txt;
    var r = confirm("Press a button!");
    if (r == true) {
        txt = "You pressed OK!";
    } else {
        txt = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

The code above is rendered as follows: