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(); });
source share