tags:

views:

420

answers:

5

Hi there,

My question is a little bit tricky ... at least i think. Maybe not... anyway. I want to know if there's any way to know if the user is leaving the page. Whatever if he clicks "Previous button", closing the window or ckicking on a link on my website. If my memory's still good, I think it's possible with Javascript. But in my case, I want to do some stuff (cleaning objects) in my codebehind. Maybe it's no possible, but my teacher learned me that's nothing is impossible ... :oP!

Thanks to repliers!

+3  A: 

There really is no way to do it. No event is fired when the browser goes back.

You can do it with Javascript, but it is difficult at best.

See the question here.

This script will also work. It was found here

<script>

window.onbeforeunload = function (evt) {
var message = ‘Are you sure you want to leave?’;
if (typeof evt == ‘undefined’) {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}

</script>
David Basarab
+3  A: 

You can use javascript to tell the server when the user leaves the page. But the webserver washes it's hands of the page once it leaves the server, while the user might keep the page open for a week.

If you use javascript on the page to fire off a notice to your server when the page unloads you can take some action. But, you can't tell if he's leaving your page for another one of your pages, another website, or closing the browser.

And that last notice isn't guaranteed to always be sent, so you can't rely on it completely.

So using the javascript notice to clean up objects (caches or sessions) is a flawed system. You're better with cache & session invalidation strategies that are independent of the onunload notice.

Tom Ritter
Nice reply. Realy clear ... especially for a guy like me that usualy talk french. Thank!
Simon
+1  A: 

As mentioned, you can do it unreliably in JavaScript, but I am assuming from the wording of your question that you want to do it in the Server-side code ("the code-behind").

That is impossible since the code-behind is running on a different computer (the web server) and has no idea what is going on at the user's computer unless the browser sends a request back to the server (which requires some Javascript or other client side code).

If you must, use the session timeout events to clean up any state information you are storing beyond the lifetime of a request.

JohnFx
+2  A: 

If the goal of this is to cleanup objects in your code-behind, is it good enough to rely on the session timeout? It would be a 20 minute delay before cleaning up those objects (or whatever you have your session timeout set for).

But it's an easy and dependable way to do it. In your Global.asax file you'd just do this:

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
  ' Clean up stuff
End Sub
Steve Wortham
Yeah good ideas in some case, but i were not clear enought in my main post I think. It's to prevent when a user came back to a page that handles a Session variable... see my second post. Thank to reply TheSteve.
Simon
A: 

Euuwww ... well ok. My problem it's because I've stored in a Session variable an objects (with a bunch of search criterias). By storing it in a session variable, if the user go back to my search page result, it will list back the same result as last time. But, after some reflexion (not in code but in my head and after seen your answer :oP), I will add a boolean property in my object which will mean something like "IsDirty" or "HasBeenListed". And when i will load back, if its set to dirty, it's gonna redirect back to another page.

Thank David.

Simon