views:

89

answers:

3

Hello Folks,

What I want to do: dispose of a session upon detecting an "OnBeforeUnload" event in the client. I know it doesn't fire 100% of times (90% accuracy works fine for me)

Here I saw how to do it with ajax, this system, however, breaks down with ajax: I can't use it at all.

I'm searching for ways of doing it

+1  A: 

In short, you can't. You can't hold a user's browser hostage, which is what this would allow as an exploit if you could do what you want (e.g. redirecting on page exit). AJAX is your only option for a server-side call, and as you noted, that's unreliable as well.

Nick Craver
I don't want to sequester the brower... but that is very easy to do: just fire an alert, confirmation or redirection after an OnBeforeUnload event. I've done it in my machine in Firefox/IE 7But I don't want to do that... I just want to, as a last action, send a flag to the server to kill the Session. Each session is over 7 MB and it must be freed if the user closes it's brower.
Lynx Kepler
What if the browser crashes? What if the Internet connection drops? What if JS isn't enabled in the first place? Forget about trying to detect when the user leaves. Just clean up inactive sessions periodically.
David Dorward
@Lynx - Your two methods of contacting a server are redirecting there and AJAX (you can't create a GET request from a new element at this point). Every platform out there times out sessions and almost all of them provide a mechanism to clean them up (ASP.Net is no different, by default it's 20 minutes). Stick with server-side cleanup, there's a reason *everyone* does it this way...don't you think if this was possible it would have been done? Also a session isn't unique to a browser window, it's shared between tabs, what if I have another tab open? This approach would kill that tab too.
Nick Craver
A: 

I do not know if it is possible to do this during a "onBeforeUnload", but maybe you could try it.
Inject a image to you web page like:

<img src="yourscript?param1=value1&param2=value2" />

From the server side just return an empty image.

Hippo
A: 

You're probably going to need to hook a synchronous call rather than AsynchronousJAX.

Dean Burge