1

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:

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

Community
  • 1
  • 1
CJ Ramki
  • 2,620
  • 3
  • 25
  • 47
  • You have to test this in many browsers. E.g. Firefox doesn't show the message created by your `return`. MSIE 10 cannot handle return false, it shows 'false'. Read the docs for `onbeforeunload`, I'm afraid you are not using it how it was meant. – hgoebl Nov 13 '13 at 10:22
  • I'm working in Firefox only... Did you see my js fiddle demo?http://jsfiddle.net/psF6m/ – CJ Ramki Nov 13 '13 at 10:26
  • 3
    Your code will also annoy users who want to close the window/tab, not just those hitting 'back' – hgoebl Nov 13 '13 at 10:35
  • I am handling web application. So user should use my application wizard controls for navigation. If User use browser navigation like back button, application session should close or logout. So, I want to get confirmation from the user to logout. So only I am trying to do this. – CJ Ramki Nov 13 '13 at 10:39
  • If you could accepting not to show a confirm button, but just be a little safer that users are logged off, then you could have a look at a solution in one of my projects: [github gist](https://gist.github.com/hgoebl/7446989) – hgoebl Nov 13 '13 at 10:42
  • I edited my previous comment. – CJ Ramki Nov 13 '13 at 10:45
  • @hgoebl, Thanks for taking effort. But, Sorry I could not understand your code... could you please show any little fiddle demo for this? – CJ Ramki Nov 13 '13 at 12:09
  • http://html5doctor.com/history-api/ – ShrekOverflow Nov 13 '13 at 13:56
  • There is any option to write 'window.onbeforeunload = function(evt) { if(return true){ aleret(true message);}else{alert(false message)}} – CJ Ramki Nov 14 '13 at 07:07

0 Answers0