Window confirm() Method - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window confirm

Description

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

Parameter Values

Parameter Type Description
message String Optional. Specifies the text to display in the confirm box

Return Value:

A Boolean, 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)

The following code shows how to Display a confirmation box:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from  ww  w  .  ja v  a2  s.c o  m*/
    var txt;
    var r = confirm("Press a button!\nEither OK or Cancel.\nThe button you pressed will be displayed in the result window.");
    if (r == true) {
        txt = "You pressed OK!";
    } else {
        txt = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials