jQuery unload event only for close window not for Link navigation

前端 未结 4 1784
清歌不尽
清歌不尽 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: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).

提交回复
热议问题