I have Example and i need to kill the session when user close his browser window. i tried page_unload() not working. the example is:i have parent windows and window will open from it i need to delete session when user close the child window. please i need some help.
-
duplicate http://stackoverflow.com/questions/920042/kill-server-side-session-on-browser-window-close-closed http://stackoverflow.com/questions/287022/closing-the-browser-should-actually-clear-the-session-variables-an-asp-net-sessio – Jeeva Subburaj Nov 04 '09 at 18:03
6 Answers
This is not something that's provided for in the normal web http protocol. There's no real way to know for sure when the browser closes; you can only "sort of" know. You have to throw together an ugly hack to get any level of certainty and even then it's bound to fail in plenty of edge cases or cause nasty side effects.
The normal work-around is to send ajax requests at intervals from the browser to your server to set up a sort of "heartbeat". When the heartbeat stops, the browser closed and so you kill the session. But again: this is a horrible hack. In this case, the main problems are that it's easy to get false positives for a failed heartbeat if the server and client to get out of sync or there's a javascript error, and there's a side effect that your session will never expire on it's own.

- 399,467
- 113
- 570
- 794
-
4+1: The only exception is if you're running some kind of in-line Java (or similar) applet that is in constant communication with the server, and the only reason that works is because it's running outside the regular HTML channel. HTML isn't a stateful protocol, and any way to try to make it so using javascript is a hack at best. – Satanicpuppy Nov 04 '09 at 18:08
-
Is this still the case, as in is there a way to check now or is it still as hacky as this? – stephano Jan 25 '21 at 00:13
-
1@stephano It's still this case. This is baked in as a fundamental piece of how HTTP works: the browser sends a request, you make a response, and no further communication can be assumed. At all. The best you can do is implement a heartbeat on your own or with a library, which again, can fail and mislead you in numerous edge cases. – Joel Coehoorn Jan 25 '21 at 01:14
-
I am trying to calculate the duration of WebRTC Calls, as there are not many good options, what are the most reliable ways of doing this with what we have? Would you: - Set up a WebSocket heartbeat where Back-End server keeps checking if front-end is connected? - Send regular updates to back-end telling the back-end that the call is still connected? – stephano Jan 25 '21 at 22:12
-
You can do this with an window.onbeforeunload handler that posts back to a sign out page.
function CloseSession( )
{
location.href = 'SignOut.aspx';
}
window.onbeforeunload = CloseSession;
This is the duplication question.. chk out here
Closing the browser should actually clear the session variables an ASP.net session id
https://stackoverflow.com/questions/920042/kill-server-side-session-on-browser-window-close-closed

- 1
- 1

- 1,881
- 2
- 18
- 26
Unfortunately this is not possible. You can find examples on the web claiming that they can do that, but they are flawed sometimes in a very subtle way.

- 7,094
- 4
- 37
- 43
There is no consistent way to detect the browser closing through javascript. If you are concerned about having too many sessions hanging, reduce the session timeout on your server. If you do that you will also want to notify your users that they need to remain active in order not to lose any work.

- 34,502
- 9
- 78
- 118
If your client-side script sent an AJAX heartbeat to your server-side app, you might use a missing beat to close the session.

- 7,315
- 26
- 33
You have reference of all child windows in parent window you need onfocus to check for child windows so when your parent get focus check if child is there or not

- 12,184
- 19
- 69
- 92