JavaScript Confirm Box
In the previous section of this tutorial JavaScript Alert Box, we discussed about alert boxes. In this section, you will learn about JavaScript confirm box and prompt box.
Confirm Box:
The confirm box is a box that pops up with both an OK and a Cancel button. The confirm box is used to verify acceptance from the user. If the user accepts, then the user presses the OK button and the confirm box returns with a true value. If the user rejects with the Cancel button, then the confirm box returns false value.
General syntax for a confirm box is
confirm (“textmessage”)
A simple confirm box example:
|
In the above example, the conditional statement if…else is used and the alert box and the confirm box are written together.
Output of the above script as produced in a HTML page is shown below:
The confirm box pops up with the message:
Wish to accept or Cancel
Showing two buttons (OK and Cancel) that the user can choose from. If the user presses OK in the confirm box then the value returned would be true, executing the if block of statements. This results in the alert box popping up with the message.
Agreed and hence true value returned.
If the user presses the Cancel button in the confirm box then the value returned would be false, executing the else block of statements. This results in the alert box popping up with the message
Not Agreed and hence false value returned
Prompt Box:
The prompt box is used when a user’s input value is desired before entering a page. The prompt box pops up with two buttons (OK and Cancel). If the user presses OK then the prompt box returns with the value entered by user but if the user presses the Cancel button then the prompt box returns with a null value.
General syntax of prompt box is as follows:
prompt(“textmessage”,”value_default”)
A simple prompt box example:
|
Output of the above script as produced in a HTML page is shown below:
In the above example, the default value is stored with value Exforsys and the prompt box pops up with the message Input the Training Company Name with a default value as Exforsys and two buttons (OK and Cancel). When the user presses the OK button, the value Exforsys is returned by prompt box, which is stored in the variable name. If the user presses Cancel, then null value is retuned by the prompt box.