Let's have a look at the code which will create a confirmation dialog box with an alert of what users click in the dialog box.
  1. <html>
  2. <body>
  3. <p>Click the button to display a confirmation box.</p>
  4. <button onclick="myBox()">Click it</button>
  5. <p id="function"></p>
  6. <script>
  7. function myBox() {
  8. var x;
  9. var y = confirm("Click a button!");
  10. if (y == true) {
  11. x = "You clicked OK button!";
  12. } else {
  13. x = "You clicked Cancel button!";
  14. }
  15. document.getElementById("function").innerHTML = x;
  16. }
  17. </script>
  18. </body>
  19. </html>
Output
After clicking the button the dialog box will be shown.
When you click the OK or Cancel button the following output will be shown.