Jump to content

How To Destroy The Session When The User Closes The Tab Not A Whole Browser?


Recommended Posts

You'll have to attach an AJAX call to the onbeforeunload event. Though, be warned:

For Javascript there is no discernible difference between reloading, navigating away from the page, or closing it; Everything you do that causes the current page to be removed/redisplayed will trigger the event!

  • 2 weeks later...

Sessions are stored on the server. More specifically, the data in a session is stored on the server. Generally, the client has only a cookie to identify what session it is using.

 

The server does not know anything about what happens on the client (the browser) until the browser sends a request. Generally, closing a tab or window or the entire browser application does NOT send any kind of request to the server. So, if you want the server to destroy the session on a page close, you have to send a message to the server. The only facility for doing that is to use AJAX in the beforeUnload event.

 

However, as Christian F. said, this event will be triggered when moving from one page to another on the same server; probably even on a hard refresh (F5 / CTRL-F5) of the same page. So, it is really not a good candidate for clearing the session.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.