The confirm box will return TRUE if the OK button is clicked and it will return FALSE if cancel button is clicked.
This confirm window will have two buttons, one for OK and other for CANCEL. Visitor can click one if the two buttons and the user action can be captured by assigning this to a variable.
We can further improve this by linking the returned value of the confirm button to an alert window.
DEMO of window confirm →
Here is the code for a confirm box linked to an alert window.
<html>
<head>
<title>Demo of Confirm window in JavaScript</title>
<script type="text/javascript">
function show_confirm() {
var my_string = confirm("Are you satisfied with the quality of the tutorials here?");
if(my_string){alert("Thank you");
}else{
alert("Please use the contact us page to give your feedbacks");}
}
</script>
</head>
<body>
<input type=button value='Click here to display confirm Window' OnClick="show_confirm()">
</body>
</html>
<script type="text/javascript">
function show_confirm() {
var my_string = confirm("Are you sure ?");
if(my_string){
document.write("You clicked OK ");
}else{
document.write("You clicked Cancel ");
}
document.write("<br><br>Back to <a href=window-confirm-demo2.php> Demo Page </a>");
document.write("<br><br>Back to <a href=window-confirm.php> Window Confirm Page </a>");
}
</script>
<input type=button value='Click' onClick=show_confirm()>
On confirmation submit or redirect page with parameters
Confirmation is taken before a delete opration. Here the id of the record is submitted to a page ( redirected ) with additional parameters through query string based on the confirmation of the user.
<script>
function show_confirm(c_id) {
var my_string = confirm("Are you sure, want to delete ? ");
if(my_string){window.location="list.php?todo=del&c_id="+c_id;}
}
</script>