Related Articles

Related Articles

Javascript | Window confirm() Method
  • Last Updated : 26 Jul, 2019

The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed.

Syntax:

confirm(message)

message is the optional string to be displayed in the dialog.

It returns a boolean value indicating whether OK or Cancel was selected (true means OK and false means that the user clicked cancel).
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Window confirm() Method
    </title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
    <h2>
            Window confirm() Method
        </h2>
    <p>Click the button to display a confirm box.</p>
  
    <button onclick="geek()">Click me!</button>
  
    <script>
        function geek() {
            confirm("Press OK to close this option");
        }
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
confirm



After clicking the button:
confirm

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Window confirm() Method
    </title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
    <h2>
            Window confirm() Method
        </h2>
  
    <button onclick="geek()">Click me!</button>
  
    <p id="g"></p>
  
    <script>
        function geek() {
            var doc;
            var result = confirm("Press a button!");
            if (result == true) {
                doc = "OK was pressed.";
            } else {
                doc = "Cancel was pressed.";
            }
            document.getElementById("g").innerHTML = doc;
        }
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
confirm
After clicking the button:
confirm

Supported Browsers: The browser supported by window confirm() method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :