The confirm()
method displays a dialog box with a specified message,
along with an OK and a Cancel button.
confirm |
Yes | Yes | Yes | Yes | Yes |
confirm(message);
Parameter | Description |
---|---|
message | Optional. Set the text to display in the confirm box. Required in Firefox. |
Type | Description |
---|---|
Boolean | A boolean value indicating whether 'OK' or 'Cancel' was clicked in the dialog box:
|
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:
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: