MOCKSTACKS
EN
Questions And Answers

More Tutorials









JavaScript window.confirm()

The window.confirm() method displays a modal dialog with an optional message and two buttons, OK and Cancel.

Now, let's take the following example:


result = window.confirm(message);

Here, message is the optional string to be displayed in the dialog and result is a boolean value indicating whether OK or Cancel was selected (true means OK).

window.confirm() is typically used to ask for user confirmation before doing a dangerous operation like deleting something in a Control Panel:


if(window.confirm("Are you sure you want to delete this?")) {
 deleteItem(itemId);
}

Output

JavaScript window.confirm() example

If you need it for later use, you can simply store the result of the user's interaction in a variable:


var deleteConfirm = window.confirm("Are you sure you want to delete this?");


  • The argument is optional and not required by the specification.

  • Dialog boxes are modal windows - they prevent the user from accessing the rest of the program's interface until the dialog box is closed. For this reason, you should not overuse any function that creates a dialog box (or modal window). And regardless, there are very good reasons to avoid using dialog boxes for confirmation.

  • Starting with Chrome 46.0 this method is blocked inside an <iframe> unless its sandbox attribute has the value allow-modal.

  • It is commonly accepted to call the confirm method with the window notation removed as the window object is always implicit. However, it is recommended to explicitly define the window object as expected behaviour may change due to implementation at a lower scope level with similarly named methods.


Conclusion

In this page (written and validated by ) you learned about Javascript window.confirm() . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Strings.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.