The link is still moving even if the user hasn’t verified

MY question is that although I click the Cancel button in the confirmation, is the link still moving to the destination? How can I stop the link from navigation to the destination if the user clicks the Cancel button in the confirmation field? I only want it to move if the user clicks the OK button:

 <a id='teachlogout' href='./teacherlogout.php'>Logout</a> 
 function logoutHandler() { if (confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) { return true; } } // logout link $('#teachlogout').click(function() { logoutHandler(); }); 
+4
source share
4 answers

You must return false to stop navigation. You can simply return what you receive from confirmation.

 function logoutHandler() { return confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) } $('#teachlogout').click(function() { return logoutHandler(); }); 

If you have confirmation in logoutHandler then put it in the click event.

 $('#teachlogout').click(function() { return confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) }); 

You can use event.preventDefault () to stop navigation.

 $('#teachlogout').click(function(event) { if(!confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n"))) event.preventDefault(); }); 
+3
source

You need to return false or event.preventDefault() if the user cancels confirm . Try the following:

 function logoutHandler() { return confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n"); } // logout link $('#teachlogout').click(logoutHandler); // ^ Shorter version when you only need to call 1 function with no params 

Or that:

 function logoutHandler(e) { if (!confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) { e.preventDefault(); } } // logout link $('#teachlogout').click(function(e) { logoutHandler(e); }); 
+8
source

Change your statement to:

 return confirm("You are currently..."); 

The problem is that you are not returning false when the user cancels the dialog.

In addition, you do not work with the return value in your handler:

 $('#teachlogout').click(function() { return logoutHandler(); // return was missing here }); 
+2
source

http://jsfiddle.net/hWe4E/

 function logoutHandler() { if (confirm("You are currently creating an Assessment, are you sure you want to logout?" + "\n" + "(Your current assessment details will be lost)" + "\n")) { return true; } return false; //added } //you need to return the true/false $('#teachlogout').click(function() { return logoutHandler(); }); 
+1
source

All Articles