JavaScript Dialog box

JavaScript provides the ability to create small window called dialog boxes. Dialog boxes appear as separate windows and their content depends on the information provided by user. Its enable us to receive input from the user and display messages to the user.

There are three types of dialog boxes is: alert, prompt and confirm.

Alert dialog box

An alert dialog box is moustly used to desplay short message to a user in a separate window. JavaScript uses alert() method of the window object to cerat alert dialog box.

The alert() method takes a sting as an argument and display an alert dialog box in the browser window.

The syntax of alert() method used to JavaScript is:
alert ("msg")
var msg = "Hello world";
Example of alert()
<script language="JavaScript" type="text/JavaScript">
alert("welcome to my website")
document.write("This is my website")

The alert() display the textual message that a user has provided as an argument and OK button.

Prompt dialog box

Prompt dialog box displays a message and an input field. This input field can be used to receive input from the user. JavaScript uses prompt() method of window object to create prompt dialog box. It is not only display a text message it also displays text box the accepts user input and OK and CANCEL button.

The syntax of prompt() method is:
prompt("message", "default value");
var name = prompt ("Enter your name:","anonymous");
Example of prompt()
<script language="JavaScript" type="text/JavaScript">
var name = prompt("Enter the name:","name");
document.write(name +"to the world of JavaScript");

Confirm dialog box

Confirm dialog box displays a predefined textual message and OK & CANCEL button to confirm user’s action. JavaScript users confirm method of window object to create confirm dialog box. Like alert and prompt dialog boxes confirm dialog box also halts the program execution until user take action by clicking OK or CANCEL button.

The syntax of confirm() method is:
confirm("Are you sure you want to exit");
Example of confirm()
<script language="JavaScript" type="text/JavaScript">
if(confirm("Open page"));
alert("same page");

Leave a Reply