0

When user accidentally closes the tab, logout function should be called. So I used window.onbeforeunload() but unfortunately, this event gets fired when you leave a site over a link or your browsers back button also by clicking on login and logout button. But what I need is only when user closes tab then user should be logged out. Is there any way I can achieve this..!!

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Pavan
  • 153
  • 3
  • 13
  • I had same issue, I'll suggest you to do it from backend, where a function should get called on session close. – Deepesh kumar Gupta Aug 23 '18 at 11:56
  • You can't detect if the user is leaving the browser or just going back to another page. The way we handled this is when the user is leaving the application, we ask if they want to leave. If they say yes we just log them out. It is better to be safe than sorry. – Mokky Miah Aug 23 '18 at 11:59
  • Which language you are using for backend code ? I am using java @deepeshkumar – Pavan Aug 23 '18 at 12:02
  • Pavan, I have used the JAVA, but it doesn't matter. every backend language has session interceptors which expose some methods on session alteration. – Deepesh kumar Gupta Aug 23 '18 at 12:05
  • But in my code during login I am setting flag to true.. after logout it is setting to false.. but when user accidentally closes the tab its not happening.. so I need to set falg to false even though user closes the tab ..@MokkyMiah – Pavan Aug 23 '18 at 12:07
  • So as soon as session closes then I need to consider it as logout ryt ? Thanks I will try this.. @deepeshkumar – Pavan Aug 23 '18 at 12:10
  • 1
    Just asking, but are you sure you need that behavior ? what if the user want to open multiple tab to use your app ? Your behavior exists natively when closing window (and not tab) while using session cookie or sessionStorage. – Jscti Aug 23 '18 at 12:12
  • @Pavan, That's the only way. What backed are you using? – Deepesh kumar Gupta Aug 23 '18 at 12:45
  • Spring framework .. java code @deepeshkumar – Pavan Aug 23 '18 at 12:57
  • good, https://stackoverflow.com/questions/3943368/invoke-method-just-before-session-expires – Deepesh kumar Gupta Aug 23 '18 at 12:58

0 Answers0