2

Possible Duplicate:
Best way to detect when user leaves a web page

I'm attemping to give a user the option to stay on the current page when they attempt to navigate away from it (if they have not saved changes to their progress). I'm unsure how to approach this as I have tried and failed JavaScript.

I'm hoping there is a way inside the Page_Unload function in ASP.NET C# code.

any help would be much appreciated.

Community
  • 1
  • 1
ErnieStings
  • 6,333
  • 19
  • 47
  • 54

6 Answers6

4

See this post "Best way to detect when a user leaves a page". This should help you out. You MUST do it client side though, as if they navigate away, the server never knows.

Community
  • 1
  • 1
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • 1
    That's hilarious. You're getting upvoted for an answer that links to a question that provides the same answer and link that I gave. Voting to close as a duplicate now that I see the other question. – tvanfosson Aug 28 '09 at 14:01
3

You can't rely on server-side code to do this. By the time the request gets to the server, it's too late. You need to do this with javascript by providing an event handler for the beforeunload event. See this demo for a simple idea on how to do it.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

I would suggest JavaScript. For example, on SO you are prompted if you attempt to exit the page after you have made some changes.

Mayo
  • 10,544
  • 6
  • 45
  • 90
1

You probably want to look at adding a javascript function to the Body OnUnload event.

CAbbott
  • 8,078
  • 4
  • 31
  • 38
  • My memory is fuzzy, but I don't think unload fires soon enough on every browser. If you could post a link that shows otherwise I'll happily delete this comment, but I think you want to look at beforeunload instead. – tvanfosson Aug 28 '09 at 13:59
  • @tvanfosson: Yeah, you're right, the user's probably looking for beforeonunload – CAbbott Aug 28 '09 at 14:06
0

I believe the best way is shown by tvanfosson above. You may inject this javascript server side providing messages, redirection etc.

-1

This is a must-have: Asp.NET page life cycle. Always keep in mind that only after all the server side work is done, the page is sent to the client.

ccalboni
  • 12,120
  • 5
  • 30
  • 38
  • What does the page life cycle have to do with getting prompted before navigating away from a page in the browser? – tvanfosson Aug 28 '09 at 13:57
  • The way ErnieStings posted his/her question shows that he/she need to understand page life cycle. I mean, it's a good thing discover how to do things, and it's better if you also understand why you are doing it that way. No one posted anything about the fact that ErnieStings was searching a way to do that job server-side, so I did. – ccalboni Aug 28 '09 at 14:13
  • 1
    The issue has nothing to do with the life cycle, though. Even if you completely understood the page life cycle, there's no way to do this server side at all (which I did note in my answer). The issue is that you have to prevent the completion of the event that's unloading the page which must be done client-side. – tvanfosson Aug 28 '09 at 14:26