Difference between window close event and back button click event in HTML?
I want to clear session when browser window closed. But when i am trying to use window.unload event, it开发者_JAVA技巧 triggers also when back button clicked. How can i avoid it?. And clear session on window close.
The other posters are correct. There is a reason you see the following setup on 99% of sites.
- Offer the users a logout button to close the session.
- Otherwise time out the session after they have been inactive for 1+ hours depending on your paranoia level.
Unfortunately clearing the session on unload() is not a good way to do it. In fact that unload handler will even fire when they go to other pages on YOUR SITE. I highly doubt this is the approach you want to go with.
There is no way to do this that I know of. It would be a security issue if you were able to tell when someone is leaving your page for another or closing the browser.
As far as your page's security model is concerned once someone is off your page there's no telling what's happening anymore
The only way to know if someone has left your site is if... they stop loading pages.
You're best bet is to track a "Last Impression" time in the session in your server application. Update it to now
every time they make a page request. If their Last Impression is ever more than, say, four hours old, you know they left for a while, so invalidate their session and start a new one for them.
精彩评论