JavaScript pop-up window
Three kinds of message boxes can be created in JavaScript: alert boxes, confirm boxes, and prompt boxes.
Alert boxes
Alert boxes are often used to make sure the user receives certain information.
When an alert box appears, the user must click OK to continue.
Syntax
window.alert("
sometext
");
window.alert() The method can be used without the window object, directly as **alert()**.
Example
Try it »
Confirm box
Confirm boxes are usually used to verify whether to accept a user action.
When a confirm box appears, the user can click "OK" or "Cancel" to confirm the action.
If you click "OK", the confirm box returns true; if you click "Cancel", it returns false.
Syntax
window.confirm("
sometext
");
window.confirm() The method can be used without the window object, directly as **confirm()**.
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 »
Prompt box
Prompt boxes are often used to ask the user to enter a value before entering the page.
When a prompt box appears, the user needs to enter a value and then click OK or Cancel to continue.
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
","
defaultvalue
");
window.prompt() The method can be used without the window object, directly as **prompt()**.
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 »
Newline
Pop-up boxes use a backslash + "n" (\n) to set a line break.
Example
alert("Hello\nHow are you?");
Try it »