JavaScript Popup Boxes
JavaScript has three types of popup boxes. They are alert box, confirm box and prompt box which are used in different contexts.
Alert Box
An alert box is used generally for alerting various messages in JavaScript. It may be a warning message or need confirmation from the user. When an alert box pops up, the user will have to click "OK" to proceed.
Eg:
<script>
alert("This is alert box!"); /* display string message */
alert(100); /* display number */
alert(true); /* display boolean */
</script>
Confirm Box
Sometimes it is necessary to take the user's confirmation for proceeding. For example, we may need to take the user's confirmation to submit a form or delete an existing data. In these cases, we use built-in function confirm(). If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Eg:
<script>
if (confirm("Press to Proceed."))
{
conf = "You pressed OK!";
}
else
{
conf = "You pressed Cancel!";
}
alert(conf);
</script>
Prompt Box
We use a prompt box when we require to take the user's input for further proceedings. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Eg:
<script>
var lucky_number = prompt("Enter your lucky number!",25);
if(lucky_number == "" || lucky_number == null || lucky_number != 25)
{
msg = "Oh! We failed to guess your lucky number!";
}
else
{
msg = "We found your lucky number successfully!";
}
alert(msg);
</script>