You’re probably looking for confirm(), which displays a Javascript Yes No Dialog and returns true or false based on what the user decided:

if (confirm('Are you sure you want to save this thing into the database?')) {
    // Save it!
} else {
    // Do nothing!

The confirm() method displays a dialog box with a specified message, along with an OK and a Cancel button.

A confirm box is often used if you want the user to verify or accept something.

Note: The confirm box takes the focus away from the current window, and forces the browser to read the message. Do not overuse this method, as it prevents the user from accessing other parts of the page until the box is closed.

The confirm() method returns true if the user clicked “OK”, and false otherwise.

<!DOCTYPE html>

<p>Click the button to display a confirm box.</p>

<button onclick="myFunction()">Try it</button>

function myFunction() {
    confirm("Press a button!");


Javascript Yes No Dialog Example:

var themeref;
var r = confirm("Press a button!");
if (r == true) {
    themeref = "You pressed OK!";
} else {
    themeref = "You pressed Cancel!";

Note: This code will not work in chrome, to make it work properly in chrome see this link and this link to clear concept about dialog.

About The Author

Related Posts

Leave a Reply

Your email address will not be published.