views:

3288

answers:

5

I need to find whether the user clicking the browser back button or Refresh button.

I need to redirect the page to Error page when he clicks the back or refresh button. How to do this.

I need to do this in javscript for my ASP.net page

+5  A: 

You can't. The browser doesn't send it's own ui events to the server. All you get are http requests and one looks pretty much like another. Maybe the clicked the back button or maybe they just retyped the last url in. Tell us what problems it's causing and we can help you adapt your project to work with the http protocol a little better.

Joel Coehoorn
+5  A: 

First of all, giving error messages if users use Back or have to refresh a page for whatever reason, is a really bad idea. Instead, you should transparently deal with that. Think about a page not coming up fully because of problems on the transportation level - the only option the user has is to reload or go back.

To answer your question, you have to keep track of the user's navigation yourself, that means on the server side. Forget about java-script here. If the user visits a website, you can store that information in a Session associated to the user (there are several methods of keeping these unique sessions, and I won't go into details here). If you store in your internal structures which pages the user visited lately, it is easy to determine a page being visited twice, or navigation going into the "wrong" direction.

You could easily generalize this (and making the whole thing more robust, for example against users jumping wildly between urls, or going back more than one step at once) by building a graph of "allowed" navigation and traversing it while the user is visiting websites.

The correct behaviour then if the user is doing a "wrong" navigation (like stepping back, reloading == visiting twice) is to get him back on track. Not to give an error message he can't escape! As he is not allowed to reload or go back, he has no options left.

ypnos
The one who gave -1 could state a reason, please!
ypnos
+1  A: 

I presume you want to do this because of post-back to avoid resubmitting some action or data right? ironically, this wasn't as huge of a problem back before asp.net webforms ... because you generally had to post to a different URL instead of the same one (much like asp.net mvc actually).

One trick I liked to use ... even though in the most technical of points is slower ... was to have an input page, and a post page. The HTML output of the post page was some super minimal HTML with a tiny javascript that replaced the current url (of the post page) in the browser's history with the result page (or even the original referring page if you really wanted). The result was when the user would click the back button, he'd be sent to the original input page, or if he clicked refresh, he'd already be on the new result page.

<html><body onload="location.replace('someURL.asp')"></body></html>
Joel Martinez
A: 

We did it in Java/Struts1 by putting a property on the submit button. If the submit button was clicked the button text would be sent in the property in the ActionForm. If the user refreshed the information the value wasn't set, so we knew the user had not clicked the button. In this case where the property was empty, we went back and rendered the first page of the pageflow. YMMV in ASP.Net.

Ed Griebel
A: 

.NET 3.5 can very well handle the browser back (and forward) buttons. Search with Google: "Scriptmanager EnableHistory". You can control which user actions will add an entry to the browser's history (ScriptManager -> AddHistoryPoint) and your ASP.NET application receives an event whenever the user clicks the browser Back/Forward buttons.

Corne