jQuery unload event only for close window not for Link navigation

前端 未结 4 1785
清歌不尽
清歌不尽 2021-01-07 13:14

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


        
相关标签:
4条回答
  • 2021-01-07 13:28

    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.

    0 讨论(0)
  • 2021-01-07 13:35

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

    0 讨论(0)
  • 2021-01-07 13:38

    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 ?

    0 讨论(0)
  • 2021-01-07 13:45

    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});   
        }
    });
    
    0 讨论(0)
提交回复
热议问题