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:
HTML
<!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:
HTML
<!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:
- 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