JS Confirm Box : Popup Box



Confirm Box in JavaScript :

  • Confirm box in JavaScript is used to confirm input provided by user.
  • It comes with two buttons (i.e “OK” and “Cancel”)
  • If user clicks on OK button then Confirm box returns “True”.
  • If user click on CANCEL button then Confirm box returns “False”.
  • The confirm() method is supported in all major browsers.
  • We can access confirm() method by using Window Object.

Syntax :

confirm()

Live Example :

<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
  {
  alert("You pressed OK!");
  }
else
  {
  alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<inputtype="button"
       onclick="show_confirm()"
       value="Show a confirm box"/>

</body>
</html>

Output :

Live Preview :

Explanation :

  1. If user clicks on “OK” then confirm box will return “true”.
  2. Then it will show alert message “You pressed OK“.
  3. If user clicks on “CANCEL” then confirm box will return False and alert message will be shown – “You pressed Cancel“.
Download Confirm Box Example : Click Here