Friday, November 15, 2024
Google search engine
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

Most Popular

Recent Comments