2

I tried window.onbeforeunload but it doesn't seem to work. Any help?

I have a cookie that tracks if the user logged in.

function handleLoginRegSuccess(){
    $.cookie('loginStatus', "ON", { expires: 1, path: '/'});
    showBusyMessage();
    goToIndex();
}

This is what I tried, but it doesnt work.

window.onbeforeunload = function(e) {
    if(userLoginStatus()=="ON"){
        requestLogout();
        $.removeCookie('loginStatus');
        return "test";
    }
};
MJB
  • 3,934
  • 10
  • 48
  • 72
  • 2
    Why do you need that? What if user just losts internet connection or power? – zerkms Nov 07 '12 at 21:32
  • I need to log the user out when he leaves the page. I see that there may be cases I cannot deal with, but oh well. if I play a game and my pc shuts down, my game is lost. happens..... – MJB Nov 07 '12 at 21:34
  • yes. So if you can live with the fact that user isn't logged out "properly" in case of power or connection lost - how about pretending that each time a user decides to close the tab - they lost connection/power? – zerkms Nov 07 '12 at 21:36
  • what are you trying to bring across? – MJB Nov 07 '12 at 21:38
  • You can not account for browser crashing, power failures, closing the lid, killing the process, etc. Session end event on the server is only way to handle it 100%. You also can not tell what fired the onbefeforunload event, it could be closing the page, refreshing, navigating away, etc. – epascarello Nov 07 '12 at 21:39
  • @MJB: I'm trying to hint that you're trying to rely on unreliable info. – zerkms Nov 07 '12 at 21:40
  • I dont want to account 100% of the cases, I want to handle the case that the user CLOSES the tab/window. whats so hard to understand about that? I know it's not 100% save, but when it solves 95% im fine. the problem is that the event didn't fire. – MJB Nov 07 '12 at 21:40
  • What is the function `userLoginStatus()`? – epascarello Nov 07 '12 at 21:41
  • @MJB: "I know it's not 100% save, but when it solves 95% im fine. the problem is that the event didn't fire." --- how about 100% safe server-side solution with pings and timeouts? – zerkms Nov 07 '12 at 21:42
  • @epascarello: it checks if the cookie exists and so what value it is set. if the value is "ON", the user is logged in – MJB Nov 07 '12 at 22:06
  • @zerkms: yeh I will take a look after it, I'm not so experienced in webdevelopment.. – MJB Nov 07 '12 at 22:13

1 Answers1

2

So, to answer your question directly, onbeforeunload needs a return value to actually work, at which point it'll prompt the user with that string and they'll get a confirm box.

More details in this stack answer: How can I override the OnBeforeUnload dialog and replace it with my own?

What you might consider instead, is to have a session timeout that logs the person out, or (more aggressively) a heartbeat from the client that keeps them logged in (via ajax). When the heartbeat fails to fire, they get logged out.

You can also use the onunload event, if you're not trying to interact w/ the user at all (as your sample code seems to not do): http://msdn.microsoft.com/en-us/library/ie/ms536973(v=vs.85).aspx

Community
  • 1
  • 1
Paul
  • 35,689
  • 11
  • 93
  • 122