0

I want to destroy session on closing tab or window.. for that I'm using onbeforeunload in jquery .. but when i open any other link from current tab then it destroys session..

so is there any specific event in jquery that is used for closing tab or window.

  • 2
    Take a look at jQuery's [.unload()](http://api.jquery.com/unload/). – Regent Sep 18 '14 at 13:29
  • You can store session in [`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#sessionStorage) which will be cleaned only after tab close. – Krzysiek Sep 18 '14 at 13:32
  • @regent it is triggered on page unload . and i wanted it on tab close – Jaydeep Pedhadiya Sep 18 '14 at 13:32
  • @krzysiek. thats what i wanted but i am not getting that tab close event so i can destroy session after triggering that event – Jaydeep Pedhadiya Sep 18 '14 at 13:33
  • @Jd what he means to say is to use Html5 session storage which will automatically handle this .. – Alok Sep 18 '14 at 13:34
  • @alok but for that i have to change my whole project session which is not appropriate for me as i have hundreds of file.. – Jaydeep Pedhadiya Sep 18 '14 at 13:37
  • have a look at this http://stackoverflow.com/questions/5292506/is-there-a-reliable-way-to-log-a-user-out-when-the-browser-is-closed – Alok Sep 18 '14 at 13:41
  • There is no guarantee that event fire. Session will destroy after window close. However, you can send ajax query every 1 minute, and set session timeout in php to 3 minutes. Without opened tab session will expire soon. – Krzysiek Sep 18 '14 at 13:42
  • okay so its impossible !! – Jaydeep Pedhadiya Sep 18 '14 at 13:45
  • Everything is possible, but not everything is beneficial ;) Even in banks you have only 15 minutes timeout and session ID holded by JS, when you close tab: JS is cleared and session is gone... (but you can use sessionStorage; simple presentation http://html5demos.com/storage ) – Krzysiek Sep 29 '14 at 08:12
  • 1
    Does this answer your question? [Detect Close windows event by jQuery](https://stackoverflow.com/questions/16707249/detect-close-windows-event-by-jquery) – Don't Panic Sep 30 '22 at 06:56

0 Answers0