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.
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
.
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.
confirm(
message)
Parameter | Description |
message | Optional. The text to display in the confirm box. |
Type | Description |
A boolean | true if the user clicked OK, otherwise false . |
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 »
confirm()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!