JS Popup Boxes
Javascript popup boxes (dialog boxes)
In JavaScript, three kinds of popup boxes – Alert box, Confirm box, and Prompt box can be created using three methods of window object.
Alert Box
Alert( ) method of window object creates a small dialog box with a short text message and “OK” command button called alert box. Alert box contains an icon indicating a warning.
Syntax
[window].alert(“Text to be displayed on the popup box”);
The word window is optional.
Example
window.alert(“I am to alert you about ….”);
or
alert(“I am to alert you about ….”);
Output
An alert box is used if we want to display some information to the user. When an alert box appears, the user needs to click “OK” button to proceed.
Confirm Box
Confirm box is used if we want the user to verify and confirm the information. The user will have to click either “OK” or “Cancel” buttons.
Syntax
[window].confirm(“Text to be confirmed”);
Example
confirm(“Do you want to quit now?”);
Output
Confirm box returns a Boolean value. If the user clicks on “OK”, it returns true. If the user clicks on “Cancel”, it returns false.
Prompt Box
Prompt box allows getting input from the user. We can specify the default text for the text field. The information submitted by the user from prompt( ) can be stored in a variable.
Syntax
prompt(“Message” [, “default value in the text field”]);
Example
var name = prompt(“What’s your name? “, “Your name please…”);
Output
A prompt box returns input string value when the user clicks “OK”. If the user clicks “Cancel”, it returns null value.