views:

522

answers:

6

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.

A: 

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.

mfeingold
A: 

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.

Dave Swersky
+10  A: 

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.

Joel Coehoorn
+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
+2  A: 

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

http://stackoverflow.com/questions/287022/closing-the-browser-should-actually-clear-the-session-variables-an-asp-net-sessio

http://stackoverflow.com/questions/920042/kill-server-side-session-on-browser-window-close-closed

Jeeva S
No guarantee that onbeforeunload will fire.
Joel Coehoorn
A: 

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

Ewan Todd
A: 

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

mamu