I want to track browser back button. If the user click on the browser back button, It should ask user to logout in pop-up confirm box. If the user press "ok", user account should log out. If user press "cancel", user should stay on the same page. I already visited:
- Confirm browser back button else stay on page
- How can I override the OnBeforeUnload dialog and replace it with my own?
- Client/JS Framework for "Unsaved Data" Protection?
- Alert user when they hit the browser back button - with good reason
But I am not getting any positive solution for my problem. I already tried this code
window.onbeforeunload = function(evt) {
if(confirm('logout ?'))
{
return;
}
else
{
alert('Dont use browser navigations');
return false;
}
}
If
part is perfectly working. But else
part, after alert box the browser shows the default dialog box.
If I use return true
instead of return
in the if
part, the browser pops up the confirm box and shows default dialog box followed that. If I use return
, It is just showing the confirm box and proceeds the action.
So, in the same way I'm using return false in else
part. If I use return false
in else
part alert box is display after confirm cancel action. Then browser's default dialog box is asking to leave the page or stay on this page.
I want to use another solution instead of return false
to make the condition false. If user click the cancel button in confirm box it should do nothing. It should stay on the same page.
See the JSFIDDLE demo