Difference between window close event and click button event in HTML?

I want to clear the session when the browser window is closed. But when I try to use the window.unload event, it fires also when I press the back button. How can I avoid this? And a clear window closing session.

+4
source share
3 answers

There is no way to do this that I know of. This would be a security issue if you could tell when someone leaves your page for another or closes the browser.

Regarding the security model of your page, when someone is disconnected from your page, it is not reported what happens more

0
source

Other posters are true. There is a reason why you see the following setting on 99% of sites.

  • Offer users a logout button to close the session.
  • Otherwise, exit the session after they are inactive for 1 hour, depending on the level of your paranoia.

Unfortunately, clearing the session on upload () is not a good way to do this. In fact, the unloading handler will fire even when you go to other pages on your site. I doubt very much that this is the approach you want to go with.

+1
source

The only way to find out if someone has left your site is if ... they stop loading pages.

Itโ€™s best to keep track of the โ€œlast impressionโ€ time in a session in your server application. Update it to now with every page request. If their last impression is more than, say, four hours, you know that they left for a while, so cancel their session and start a new one for them.

0
source

Source: https://habr.com/ru/post/1337779/


All Articles