9
  1. User Visits Page A and clicks around a few times, adjusting page-state as he/she goes.
  2. Page A eventually redirects user to Page B
  3. User clicks browser back button, sees Page A again in browser

I need to perform some interface clean-up on step 3.

How do I capture this event in Page A?

Jeffrey Blake
  • 9,219
  • 5
  • 40
  • 64
Brian Webster
  • 27,545
  • 47
  • 143
  • 218
  • 2
    Here is a similar post that covers using jQuery and the back-button : http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button – Rion Williams Aug 11 '11 at 15:34

3 Answers3

2

I'm going to make a few assumptions to present a way to do this that isn't fully reliant on javascript:

  1. You noted in the comments to another answer that this is an ASP.NET app. I am going to assume that you only care about the back button where it is being pressed from another page in your ASP.NET app.
  2. You control the server-side code executed in the ASP.NET code-behind for both the page in question and for the page(s) it will need to "back" out of.

If both of those conditions are true, then you should be able to use a Session variable! Just set the current page into a currPage variable maintained by session on all of the related pages. Then when the page is loading, check that and use it to either adjust the way the page is shown directly, or to write a value to a hidden field/input/div, which your javascript can read and use to react appropriately.

If you go the JS-read-and-execute route, you'll want to test to ensure the events fire properly on all browsers after a load-from-back-button. As mentioned in the link from Rionmonster, sometimes onload events may not fire after a back-button. If you see issues from this, then use the tricks mentioned there to get around it.

This type of solution should work for any web-app that employs server-side code capable of interacting with Session variables (e.g. it will also work for PHP, Python, and many others).

Note: There's a chance this won't be quite as elegant as it sounds. In a lot of cases browsers may not actually pull a fresh page from the server after the back button is pressed. If testing reveals this to be an issue, you may need to employ some AJAX techniques to populate the information fresh to the JS after the back button-press.

Community
  • 1
  • 1
Jeffrey Blake
  • 9,219
  • 5
  • 40
  • 64
1

Aside from possibly setting a cookie, you can't due to a browser security restriction. A work around would be to check if the user navigates away from the page via onbeforeunload or onunload fire.

RobB
  • 8,408
  • 1
  • 23
  • 34
1

Would it be possible in your scenario to use the URL hash to navigate to page B and use AJAX and jQuery to load the new page? Therefore, the browser is not actually leaving the page. When the user navigates back, all they will do is change the URL hash back. You could then capture the hashchange event to see if the user has navigated back to page A.

Connell
  • 12,845
  • 9
  • 53
  • 84
  • Connel, I like the idea, but I think our web app will be too much of a heavy-weight for this method. It's an ecommerce app within DotNetNuke with ASP.NET Web Forms – Brian Webster Aug 11 '11 at 15:55
  • Ah, always worth the suggestion though! Have you tried the accepted answer in the post that @Rionmonster suggested? – Connell Aug 11 '11 at 15:58
  • Not yet, need some time to analyze. It looks promising – Brian Webster Aug 11 '11 at 15:59
  • I'm the slightly-more-javascript-adept coworker of hamlin11. The problem with the method in Rionmonster's link is that it simply enables onload code to execute after back is pressed in addition to on the first page load. We need something that *only* executes as a result of the back button being pressed. – Jeffrey Blake Aug 12 '11 at 13:20
  • Well expanding on my solution then. Sort of cheating a little (but hey, sometimes there just isn't a perfect solution!), but a possibility would be to change the hash tag of page B onload, when the back button is pressed you could capture this hash change, then force the browser to navigate back again to page A using JavaScript. You could do whatever it is you need to do in this function, or if it is server side you could pass something back in the query string to page A. – Connell Aug 12 '11 at 14:21
  • If you're worried about an ugly looking URL for page B, you could even change the hash tag twice on load, once to the specific hash, then back to nothing, and capture the change to the specific hash. But then you're getting real dirty ;) – Connell Aug 12 '11 at 14:22