JavaScript Tutorial

BOM Popup Alert (Popup Boxes)

JavaScript has three kinds of popup boxes:

  1. Alert box
  2. Confirm box
  3. Prompt box

Alert Box

An alert box is generally used if you want to make sure information comes to the user.

When an alert box pops up, the user will have to click "OK" to proceed.

window.alert("write something");

The window.alert() method can be used without the window prefix.

Input:-

Output (Before Click):-

Output (After Click):-

Confirm Box

A confirm box is generally used to verify or accept something.

When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.

If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

window.confirm("My confirm box");

The window.confirm() method can be written without the window prefix.

Input:-

Output (Before click the button):-

Output (After click the button):-

Output (After pressed OK in confirm box):-

Prompt Box

A prompt box is generally used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

window.prompt("myText","defaultText");

The window.prompt() method can be written without the window prefix.

Input:-

Output (Before click the button):-

Output (After click the button):-

Output (After pressed OK in prompt box):-

Line Breaks

To display line breaks inside a popup box, use a back-slash followed by a period.

Input:-

</html>

Output (Before click):-

Output (After click):-


Go back to Previous Course