I have a link which displays a confirm box when user click on it. If the user click yes then it will lead them to the specified page, otherwise it stays on the same page. Here is my link and my function code:
<script type='text/javascript'>
function checkDelete(){
var i = confirm("Are you sure you want to delete?");
if(i){
return true;
}else{
return false;
}
}
</script>
<a href="operation.php?action=delete&id=<?php echo $id?>" onclick='checkDelete'>Delete</a>
The problem is that it does not stay on the same page even I click “No” or cancel button. It always redirect to the page operation.php. But if I write inline javascript code on the onclick even then it works fine.
Try
onclick="return checkDelete();". Using a proper JavaScript expression kind of helps when you try to use JavaScript 😉At the same time, try changing
checkDeleteto this: