Search Your Project Here Please

Explain alert(),prompt(),and confirm() dialog box in JavaScript with example?


Explain alert(),prompt(),and confirm() dialog box in JavaScript with example?
Alert dialog box: An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.

Syntax:

window.alert("sometext");
Example:

<html>
<head>
<script>
function myFunction()
{
alert("I am an alert box!");
}
</script>
</head>
<body>

<input type="button" onclick="myFunction()" value="Show alert box">

</body>
</html>
Confirm dialog box:
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will 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:

         window.confirm("sometext");
Example:

var r=confirm("Press a button");
if (r==true)
  {
  x="You pressed OK!";
  }
else
  {
  x="You pressed Cancel!";
  }
Prompt dialog box:
A prompt box is often used if you want the user to input a value before entering a page.
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

window.prompt("sometext","defaultvalue");
Example:
var person=prompt("Please enter your name","Harry Potter");
if (person!=null && person!="")
  {
  x="Hello " + person + "! How are you today?";
  }

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...