Warning boxes are nothing but the JavaScript Popup boxes. They are used to warn the users by showing/alerting the additional information.
Confirm Box contains two buttons by default
OK and
Cancel. The text entered will tell the user whether to accept or verify. If clicked OK, button returns TRUE, else returns false. if…else statements are used in Confirm boxes.
Syntax for confirm box is as follows.
confirm(“your text”);
The following is an example for Confirm Box.
[html]
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<p id="test"></p>
<script>
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
x = "You pressed OK!";
} else {
x = "You pressed Cancel!";
}
document.getElementById("test").innerHTML = x;
}
</script>
</body>
</html>[/html]
Output:
When the button is clicked, a message appears showing the button that was clicked.
Prompt box is used whenever some information has to be inputted by the user before entering the page. Here, two buttons appear-OK and Cancel. If clicked OK, the prompt box will return the information entered into the text box, else nothing will be displayed and the user will be taken to a page.
Syntax for prompt box is as follows.
prompt(“Your text”,”default text”);
Below is an example for Prompt Box.
[html]
<!DOCTYPE html>
<html>
<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
<p id="test"></p>
<script>
function myFunction() {
var person = prompt("Please enter your name", "Mike");
if (person != null) {
document.getElementById("test").innerHTML = "Hello " + person + "! Welcome to SPLessons";
}
}
</script>
</body>
</html>[/html]
Output:The output appears as follows. As the default value is Mike, it was shown in the box. When clicked OK, the output will be "Hello Mike! Welcome to SPLessons".