Javascript | Window confirm() Method
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:
<!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> |
Output:
Before clicking the button:

After clicking the button:

Example-2:
<!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> |
Output:
Before clicking the button:

After clicking the button:

Supported Browsers: The browser supported by window confirm() method are listed below:
- Apple Safari
- Google Chrome
- Firefox
- Opera
- Internet Explorer
Recommended Posts:
- Javascript | Window Blur() and Window Focus() Method
- Javascript | Window Open() & Window Close Method
- JavaScript | Window print() Method
- Javascript | Window scrollTo() Method
- JavaScript | Window getComputedStyle() Method
- Javascript | Window prompt() Method
- JavaScript BOM | Window Screen
- How to close current tab in a browser window using JavaScript?
- JavaScript | Window innerWidth and innerHeight Properties
- How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
- HTML | DOM Window stop() Method
- HTML | Window moveTo() Method
- HTML | Window resizeTo() Method
- HTML | Window resizeBy() Method
- HTML | Window btoa( ) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



