Hello Readers
Javascript has three kind of popup boxes:
- Alert box
- Confirm box.
- Prompt box.
- Alert box:
If you want the information comes through the user then we use the alert popup box.
Syntax of alert box:
window.alert("sometext");
It can be also used without window prefix.
For Example:
<html>
<body>
<p>Click the button to display an alert box:</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("I am an alert box!");
}
</script>
</body>
</html>
- Confirm Box:
If you want the user to verify or accept something then we use the confirm box. So, when the confirm box pops up user have to click either "ok" or "cancel" to proceed.
If the user clicks "ok", the box returns true. If the user clicks "cancel", the box returns false.
Syntax of confirm box:
window.confirm("sometext");
We also use window.confirm() method without the window prefix.
For Example:
var r = confirm("Press a button");
if (r == true) {
x = "You pressed OK!";
} else {
x = "You pressed Cancel!";
}
- Prompt Box:
If you want the user to input a value before entering into the page then we use the prompt box. So, when a prompt box pops up, the user will have to click either "ok" or "cancel" to proceed after entering an input value. If the user clicks "ok" the box returns the input value if the user clicks "cancel" the box returns null.
Syntax of prompt box:
window.prompt("sometext","defaultText");
We also use window.prompt() method without the window prefix.
For Example:
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML ="Hello " + person + "! How are you today?";
}
0 Comment(s)