Using the confirm() Method
The confirm() method works similarly to alert(), but this box is used to give the user a choice between OK and Cancel. You can make it appear in pretty much the same way, with the only difference being that instead of using the alert() method, you use the confirm() method.
Note - The Cancel button needs more JavaScript behind it than you have covered so far to make it work. Fear not, though; it will be covered!
So again, following the same routine as for the alert() method, you begin by adding the confirm() method to the script block as follows:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-- Cloaking device on! confirm(); // Cloaking device off --> </script> </head> <body> </body> </html>
And again, the message you want to appear in the box is typed inside the parentheses, contained in quotes:
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-- Cloaking device on! confirm("Which one will you choose?"); // Cloaking device off --> </script> </head> <body> </body> </html>
Save the file (again under a different name from the template and remembering to use the HTM or HTML file extension) and load it into the browser (see Figure 3.8).
Now notice the buttons on the boxOK and Cancel. However, at the moment nothing happens when you click them except that the box disappears and the JavaScript continues to run again. Before you can use the buttons on the confirm box, you will need a few more JavaScript skills, after which you will revisit the confirm() method.
Exercise - Practice adding the confirm() method to a Web page. Add your own messages.
Add a couple of separate confirm() methods to a Web page and note the result.