Dialog Box in javaScript with Examples

Dialog Boxes in javaScript:

JavaScript supports three types of dialog boxes:


1) Alert dialog box:

An alert dialog box is used to give a warning message to the users. It pops up a message box dispalaying some contents with a OK button. JavaScript alerts are used in the following situations:

  • to see a message before doing anything on the website.
  • to warn the user abut something.
  • It can be used as an error indication.
  • To confirm some action.

Example :

Result:




2) Prompt dialog box:

The prompt dialog box is very useful when a pop-up text box to used to get user input. Thus it enables to interact with the user. The user needs to fill 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.
  • (ii) A default string to display in the text box.
  • This dialog box with two buttons: OK and Cancel. If the user clicks on Ok button the window method prompt() will return entered value from the text box. If the user clicks on the Cancel button the window method prompt() returns null.

    Example:

    Result:

    	
    


    3) Confirmation dialog box:

    A confirmation dialog box is mostly used to take user's consent's on any option. It displays a dialog box with two buttons:OK and Cancel. If the user clicks on OK button the window method confirm() will return true. If the user clicks on the Cancel button confirm() returns false.

    Example:


    Result: