WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Popup Boxes


You can create three message boxes in JavaScript: warning boxes, confirmation boxes, and prompt boxes.


Warning box

Warning boxes are often used to ensure that certain information is available to users.

When the warning box appears, the user needs to click the OK button to proceed.

Syntax

window.alert("sometext");

The window.alert () method can directly use the alert () method without a window object.

Example

alert("I am a caution box!");
Try it yourself »

Confirmation box

A confirmation box is usually used to verify whether a user action is accepted.

When the confirmation card pops up, the user can click "Confirm" or "Cancel" to confirm the user operation.

When you click "OK", the confirmation box returns true. If you click "Cancel", the confirmation box returns false.

Syntax

window.confirm("sometext");

The window.confirm () method can directly use the confirm () method without a window object.

Example

var r = confirm ("Press the button"); if (r == true) { x = "You pressed the" OK "button!"; } else { x = "You pressed the" Cancel "button!"; }
Try it yourself »

Prompt Box

The prompt box is often used to prompt the user to enter a value before entering the page.

When the prompt box appears, the user needs to enter a value and then click the confirm or cancel button to continue the operation.

If the user clicks OK, the return value is the value entered. If the user clicks Cancel, the return value is null.

Syntax

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

window.prompt () method can directly use the prompt () method without a window object.

Example

var person = prompt ("Please enter your name", "Harry Potter"); if (person! = null && person! = "") { x = "Hello" + person + "! How are you feeling today?"; document.getElementById ("demo"). innerHTML = x; }
Try it yourself »

Line Breaks

The popup uses backslash + "n" (\ n) to set a newline.

Example

alert("Hello\nHow are you?");
Try it yourself »