Window confirm() method
- Previous Page closed
- Next Page console
- Go Back to the Previous Level Window Object
Definition and Usage
confirm()
The method displays a dialog box containing a message, an OK button, and a Cancel button.
If the user clicks "OK",confirm()
The method returns true
, otherwise return false
.
Description
Confirmation boxes are usually used when you want the user to verify or accept something.
The confirmation box will remove the focus from the current window and force the user to read the message.
It will prevent the user from entering any input into the browser until the user clicks the OK or Cancel button to close the dialog box. confirm()
It will pause the execution of JavaScript code until the user responds, and the next statement will not be executed.
See also:
Instance
Example 1
Display the confirmation box:
confirm("Press a button!");
Example 2
Confirmation box with a newline character:
confirm("Press a button!\nEither OK or Cancel.");
Example 3
Display the confirmation box and output the content clicked by the user:
let text; if (confirm("Press a button!") == true) { text = "You pressed OK!"; } else { text = "You canceled!"; }
syntax
confirm(message)
parameter
parameter | Description |
---|---|
message | Optional. The text to be displayed in the confirmation box. |
Return Value
Type | Description |
---|---|
Boolean Value | If the user clicks OK, it returns true, otherwise it returns false. |
Browser Support
All Browsers Support confirm()
:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous Page closed
- Next Page console
- Go Back to the Previous Level Window Object