JavaScript Pop up Boxes

Popup boxes are useful when we need to display small message to the user or required some confimation from user. There are total 3 types of pop up boxes avaiablble in JavaVcript alert, confirm and prompt.

Alert Box

When we want to display some messages to user then alert box can be used. Like for validation we want to display that "Please enter firstname" then alert box can be used as below code. For example

Example
var firstName = "";
if(firstName == "")
{
	alert("First name is mandatory, please enter first name");
}

Confirm Box

When we want to ask user for confirmation then confirmation box can be used. It will have two buttons "ok" and "cancel". If user clicks ok it will return true else false. So let's say before doing something we want to confirm from enduser then confirm box will be used as below. For example

Example
if(confirm("Are you sure you want to delete this record?"))
{
	alert("Thank you for the confirmation");
}

Prompt Box

Sometimes we need to take single input from user in text form then prompt box can be used. It will return the value whatever user has entered into the prompt box. For example

Example
var favColor = prompt("What's your favourite colour?");
alert("Your favourite colour is: " favColor);

Info: Prompt box has another optional second argument, which will receive as text and that will be displayed in the textbox in the prompt box. So if we want to set some default value into prompt box then we can use second parameter.

 

← Previous

Next →