How to logout my application when I closed the window?

后端 未结 9 1932
猫巷女王i
猫巷女王i 2020-12-15 13:33

In my chat application i am having the logout button and it works fine.

Now I need to logout the application when I closed the browser window also..How can I achieve

9条回答
  •  佛祖请我去吃肉
    2020-12-15 13:46

    For this issue I tried 2 solutions: window.onbeforeunload event and sessionStorage

    • Since window.onbeforeunload is not only for closing the browser but also redirect, tab refresh, new tab, it was not a robust solution. Also there are cases which the event does not happen: closing the browser through the command line, shutting down the computer

    • I switched to using sessionStorage. When the user logs in I set a sessionStorage variable to 'true'; when the application is loaded I would check to see if this variable is there, otherwise I would force the user to log in. However I need to share the sessionStorage variable across tabs so that a user is not forced to log in when they open a new tab in the same browser instance, I was able to do this by leveraging the storage event; a great example of this can be found here

提交回复
热议问题