jQuery unload event only for close window not for Link navigation

本秂侑毒 提交于 2019-12-19 04:55:21

问题


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;
  });

回答1:


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});   
    }
});



回答2:


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).




回答3:


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.




回答4:


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 ?



来源:https://stackoverflow.com/questions/19565235/jquery-unload-event-only-for-close-window-not-for-link-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!