English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Window confirm() Method

JavaScript Window Object

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).

Syntax:

window.confirm(msg)
var r = confirm("Click the button!");
if (r == true) {
   txt = "Clicked Confirm!";
} else {
   txt = "Clicked Cancel!";
}
Test and see‹/›

Browser Compatibility

All browsers fully support the Confirm() method:

Method
confirm()YesYesYesYesYes

Parameter Value

ParameterDescription
msg(Optional) The string displayed in the confirmation box

Technical Details

Return Value:A boolean value indicating whether "OK" or "Cancel" was selected
  • true-The user clicks "OK"

  • false-The user clicks "Cancel" (or the "x" (close) button in the upper right corner, available in all major browsers except Firefox)

More Examples

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:


Related References

Window (Window) Reference:alert() method

Window (Window) Reference:hint() method

JavaScript Window Object