JavaScript Popup
You can create three types of message boxes in JavaScript: alert boxes, confirmation boxes, and prompt boxes.
Alert Box
Alert boxes are often used to ensure that information reaches the user.
When an alert box pops up, the user must click the OK button to proceed.
Syntax
The window.alert() method can be used without the window object, directly using the alert() method.
Example
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("Hello, I am an alert box!");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="Show Alert Box">
</body>
</html>
Confirm Box
Confirm boxes are typically used to verify whether the user wants to accept an action.
When a confirm box pops up, the user can click "OK" or "Cancel" to confirm the action.
If you click "OK", the confirm box returns true. If you click "Cancel", the confirm box returns false.
Syntax
The window.confirm() method can be used without the window object, directly using the confirm() method.
Example
Prompt Box
Prompt boxes are often used to prompt the user to enter a value before entering the page.
When a prompt box pops up, the user needs to enter a value and then click OK or Cancel to proceed.
If the user clicks OK, the return value is the entered value. If the user clicks Cancel, the return value is null.
Syntax
The window.prompt() method can be used without the window object, directly using the prompt() method.
Example
var person = prompt("Please enter your name", "Harry Potter");
if (person != null && person != "") {
var x = "Hello " + person + "! How are you today?";
document.getElementById("demo").innerHTML = x;
}
Line Breaks
Popups use the backslash + "n" (\n) to set line breaks.
Example
alert("Hello\nHow are you?");