Sure! Here's a complete guide with code examples for JavaScript pop-up boxes that you can print out:
JavaScript Pop-up Boxes: A Complete Guide with Code Examples
- Alert Box
An alert box is a pop-up window that displays an alert message to the user. It has a single "OK" button and is used to inform the user about a situation or event.
Code Example:
javascriptalert("Hello, World!");
This will display an alert box with the message "Hello, World!" and an "OK" button.
- Confirm Box
A confirm box is a pop-up window that displays a message to the user and has two buttons: "OK" and "Cancel". It is used to ask the user to confirm or cancel an action.
Code Example:
javascriptvar result = confirm("Are you sure you want to delete this item?");
if (result) {
// User clicked "OK"
deleteItem();
} else {
// User clicked "Cancel" or closed the dialog box
}
This will display a confirm box with the message "Are you sure you want to delete this item?" and "OK" and "Cancel" buttons. If the user clicks "OK", the deleteItem()
function will be called. If the user clicks "Cancel" or closes the dialog box, nothing will happen.
- Prompt Box
A prompt box is a pop-up window that prompts the user to enter data. It has an input field for the user to enter data, as well as "OK" and "Cancel" buttons. It is used to get input from the user.
Code Example:
javascriptvar name = prompt("What is your name?");
if (name != null) {
// User clicked "OK" and entered a value
greetUser(name);
} else {
// User clicked "Cancel" or closed the dialog box
}
This will display a prompt box with the message "What is your name?" and an input field for the user to enter their name. If the user clicks "OK" and enters a value, the greetUser()
function will be called with the entered name as a parameter. If the user clicks "Cancel" or closes the dialog box, nothing will happen.
That's it! These are the three types of pop-up boxes in JavaScript. You can use them to create interactive and informative user experiences on your web pages.