JavaScript Dialog Boxes - Java Script

In this Chapter, You will learn about Dialog boxes in JavaScript. The Dialog boxes in JavaScript is used to alertand raise , or to get a confirmation from the user on the input or to have a kind of input from the users.JavaScript has three kind of dialogue boxes. They are:

  • Alert dialog box
  • Confirmation dialog box
  • Prompt dialog box

Let us discuss each one in detail.

What is an Alert Dialog box?

An alert dialog box is often used in order to give warning message to the user.For instance, if atext field requires to enter some input but the user does not gave any input, at that time as a part of validation, one can make use of an alert box to give a warning message.

However, an alert Dialog box can still be used foruser friendly messages. Alert box gives only one button "OK" to select and continue.



What is confirmation dialog box?

A confirmation dialog box is often used to take user’s permission on any option. Generally, a confirmation dialog box appears with two buttons: Cancel and Ok.

When confirmation box appears on the screen the user have to click either Ok or Cancel. On clicking OK button, the window.confirmmethod will return true on the other hand on clicking Cancel button window.confirmmethod returns false. The below example illustrates how to use a confirmation dialog box -



What is a prompt dialog box?

The prompt dialog box is most commonly used if one wants the user to input a value before entering a page. Thus, this facilitates to communicate with the user. The user have to input a value in the field and then click OK.

This dialog box is displayed using a method called prompt() which takes two parameters:

(i) a label which you want to display in the text box and

(ii) a default string to display in the text box.

When the prompt dialog box appears on the screen,the user have to click either Ok or Cancel. On clicking OK button, the window.promptmethod will return the input value which is entered by the user. On clicking the Cancel button, the window.prompt method returns null.


The below example illustrates how to use a prompt dialog box −


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Java Script Topics