Friday, October 17, 2025
HomeLanguagesJavascriptJavascript Window confirm() Method

Javascript Window confirm() Method

The confirm() method is used to display a dialog box 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)

Parameters:

  • message: It 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: This example shows the working of the window.confirm() function, A dialog box opens with the OK and Cancel button.

HTML




<body style="text-align: center;">
    <h1 style="color:green;">
        neveropen
    </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>


Output: 

 

Example 2: This example shows the working of the window.confirm() function, A dialog box opens with the OK and Cancel button and prints the values after the button is clicked.

HTML




<body style="text-align: center;">
    <h1 style="color:green;">
        neveropen
    </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>


Output: 

 

We have a complete list of HTML DOM methods, to check those please go through the DOM Complete Reference article.

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

  • Google Chrome 1 and above
  • Edge 12 and above
  • Internet Explorer 4 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above
RELATED ARTICLES

1 COMMENT

  1. […] Confirm Dialog Box: A confirm dialog box is used to take the user’s confirmation option. It displays a dialog box with two buttons: OK and Cancel. For example, if a user deletes some data, the page can confirm it using a confirm box as if really that data is to be deleted. If the user clicks on the OK button, the method returns true value whereas if the user clicks on the Cancel button, then confirm() method returns false.  […]

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS