English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
confirm()The method displays a modal dialog box with an optional message and two buttons, "OK" and "Cancel".
If the user clicks "OK", the confirm() method will return true; otherwise, it will return false.
Confirmation boxes are usually used when you want the user to verify or accept certain content.
Dialog boxes are modal windows-They prevent the user from accessing the rest of the program interface before the dialog box is closed. Therefore, you should not overuse any functions that create dialog boxes (or modal windows).
window.confirm(msg)
var r = confirm("Click the button!"); if (r == true) { txt = "Clicked Confirm!"; } else { txt = "Clicked Cancel!"; }Test and see‹/›
All browsers fully support the Confirm() method:
Method | |||||
confirm() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
msg | (Optional) The string displayed in the confirmation box |
Return Value: | A boolean value indicating whether "OK" or "Cancel" was selected
|
---|
Confirm box with line breaks:
confirm("Press a button\nEither OK or Cancel.");Test and see‹/›
This example demonstrates the different types of dialog boxes supported by JavaScript:
Click the button below to display different dialog boxes:
Window (Window) Reference:alert() method
Window (Window) Reference:hint() method