JavaScript Popup Dialog Boxes

JavaScript has three types of popup windows or dialogs:
  • Alert window
  • Confirmation window
  • Prompt window (Prompt dialog)

Alert window

A dialog box is used if you want to display information to the user. When the dialog appears, the user clicks "OK" to continue.

Example:
method alert()

window.alert("message"); 

Confirmation window

A confirmation dialog box is used if you want the user to confirm a choice. When the confirmation box appears, the user has the choice to click "OK" or "Cancel" to continue.
If the user clicks "OK", the operation returns "true". If the user clicks "Cancel", the operation returns "false".

Example:
confirm javascript method

if (confirm("Make a choice!")) {
  txt = "You clicked OK!";
} else {
  txt = "You canceled!";
}

Prompt window

A prompt dialog box is used if you want to give the user the option to fill in or enter a text field of type input, for example.
When the window appears, the user must fill in this field and click "OK" or "Cancel" to proceed.
Example:
javascript prompt method

var person = prompt("Please enter your first and last name", ""); 
if (person== null || person== "") {
msg= "You quit";
} else {
msg= "Hello" + nobody + "! How are you?";
}