0

I am using this code for logging out user when closes the page, but the user will logout also when clicking on other links (same website):

  $( window ).unload(function() {
    $.ajax({url:"?logout&leave=yes", async:false})
  });

Is there any way to distinguish between link navigation and real page close?

EDIT:

I am currently implemented this solution, but it lacks to detect page reload

  $('a').click(function(){
      var url = $(this).attr("href");
      window.onbeforeunload = null;
      $(window).unbind('beforeunload');
      window.location = url;
  });
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jason OOO
  • 3,567
  • 2
  • 25
  • 31

4 Answers4

5

Try the following solutions, hope this helps

<script>
$(window).bind('click', function(event) {
    if(event.target.href) 
        $(window).unbind('beforeunload');
});
$(window).bind('beforeunload', function(event) {
    $.ajax({url:"?logout&leave=yes", async:false});
});
</script>

or

var logOutFlag = true;
$('a').click(function(){
    logOutFlag = false;
});
$(window).bind('beforeunload', function(event) {
    if(logOutFlag){
         $.ajax({url:"?logout&leave=yes", async:false});   
    }
});
N20084753
  • 2,130
  • 2
  • 17
  • 12
  • will this work if the user clicks a `submit` button and the page posts back? – th1rdey3 Oct 26 '13 at 20:21
  • 1
    @th1rdey3 - ya this should work i hope since on submit action you can make the flag to be false or unbind beforeunload, then only the beforeunload event will be triggered such that you can prevent the logout. – N20084753 Oct 26 '13 at 20:27
4

This question has been asked several times :

javascript detect browser close tab/close browser
How to detect a window close event using javascript or php
Automatic logout if the tab is closed
and you can find many others.

The short answer is : you can't. As the other answers say : there are actually many other legitimate cases where the user definitely doesn't want to be logged out when closing a tab (same site opened in two tabs, bookmark clicking, url manually modified in the navigation bar ...).

The only way I know of detecting if a user wants to log out is to put a "Logout" button on your page.
Otherwise, you should manage a timeout mechanism for your sesions (e.g. : on the server side, delete sessions after say 30 minutes of inactivity).

Community
  • 1
  • 1
LeGEC
  • 46,477
  • 5
  • 57
  • 104
1

There is no way to know where does the user want to go when he leaves your page.

Instead on unbinding manually events on click, you could set a flag, and skip AJAX request in your unload callback when this flag is set. Also, note that $(window).unload() is deprecated and should be replaced with: $(window).on('unload', function() {}).

The only solution I can think about, is to set value in a cookie (or LocalStorage) on page unload and check if this value is set on page load. But that won't work if you want to send an AJAX request only when the users leaves your app.

I am afraid we are in front of an XY problem, though. Can't you set some sort of timeout on server-side, which will properly logout user as soon as he is inactive? Even with the link trick, your current solution won't work if the user is logged in multiple times, if he opens a new window/tab, or if he simply lets the page run in background.

Community
  • 1
  • 1
Guillaume Poussel
  • 9,572
  • 2
  • 33
  • 42
0

Links arent the only problem, what if user edits the url and resubmits or simply refreshes the page to see new content. Or as mentioned previously, if user has 2 or more windows opened, and he closes the one, does it log out the other window ?

Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42