Home : Internet : JavaScript : Confirm

JavaScript Confirm

A JavaScript confirm box is similar to an alert box, except that the user is given two choices — OK and Cancel. You can't change the names of the choices but you can determine what they do.

Here is an example of the code used:

<SCRIPT type="text/javascript">
function askquestion() {
   if (confirm("Do you wish to proceed to the entry page?") ) {
      parent.location='http://www.example.com/enter.html';
      } else {
      parent.location='http://www.example.com/exit.html';
   }
}
</SCRIPT>

<input type="button" value="Click here to see a confirm dialogue box" onClick="askquestion();">

The script uses an IF/ELSE statement to determine what to do. In this example, if the user clicks OK they are taken to an entry page. If they click Cancel they are taken to a different page.

You can change the code to do whatever you want for each option.