Types of Pop up boxes available in JavaScript

JavascriptFront End TechnologyObject Oriented Programming

There are 3 types of pop up boxes available in JavaScript. These are −

1. Alert − The Window.alert() method displays an alert dialog with the optional specified content and an OK button.

For example, if you execute the following script, it'll open an alert box with the content: "This is an alert" with a confirmation button.

Example

 Live Demo

<script>
   alert("This is a alert");
</script>

Note that the alert dialog should be used for messages which do not require any response on the part of the user, other than the acknowledgement of the message.

2. Confirm − The Window.confirm() method displays a modal dialog with an optional message and two buttons: OK and Cancel.

For example, if you execute the following script, it'll open a confirmation box with the content: "Please confirm this action" with a confirmation button and cancellation button. This returns a boolean depending on the input provided by the user.

Example

 Live Demo

<script>
   let bool = confirm("Please confirm this action");
   console.log(bool);
</script>

If you click confirm, it'll return true. If you click cancel, it'll return false.

Note that Dialog boxes are modal windows — they prevent the user from accessing the rest of the program's interface until the dialog box is closed.

3. Prompt − The Window.prompt() displays a dialog with an optional message prompting the user to input some text.

For example, if you execute the following script, it'll open a propmt box with the content: "Please enter your name" with a confirmation button and cancellation button. This returns a String provided by the user.

Example

 Live Demo

<script>
   let name = prompt("Please enter your name");
   console.log(name);
</script>

When you give it some input in the prompt, it'll log your name to the console.

raja
Published on 16-Sep-2019 11:53:37
Advertisements