7

Suppose I have a page open in a browser and I go to my address bar and enter another page. Then I hit the back button to go to my original page. I'd like to write some Javascript code that can detect this scenario and respond to it.

As best I can tell neither the ready event, the onload, nor any inline Javascript on the page itself is re-executed in this scenario. Is there anything else I can do?

Cross-browser support is important here. jQuery based solutions preferred but not required.

Edit for clarity: the navigation I'm assuming is Page A -> Page B -> back to page A where I'm assuming that Page B may or may not be under my control.

Dan
  • 10,990
  • 7
  • 51
  • 80

2 Answers2

0

You should try to give the user a cookie on both pages, the navigated to page and the page itself, with dates and times and compare see if they're close, or if they show that he's been on one page, been on another, and then redirected.

Another option is to give the user a cookie when he is redirected

Omer Tuchfeld
  • 2,886
  • 1
  • 17
  • 24
  • I'm assuming the navigated-to page is not under my control. I'll update my question. – Dan Jun 13 '11 at 15:57
  • Does the navigated page redirect the user to your page? or do you want to check if the user clicked the "Back button"? Can you provide the navigating website with a URL? because maybe you can give it a special URL – Omer Tuchfeld Jun 13 '11 at 15:59
  • No the scenario I mean is a) user goes to my page, b) user goes into the address bar and enters some other page, c) user clicks the back button. I want to detect c). – Dan Jun 13 '11 at 16:04
0

In Firefox, you can check for the DomContentLoaded event. For a cross-browser solution, a little more work is required:

http://dean.edwards.name/weblog/2005/09/busted/

George Cummins
  • 28,485
  • 8
  • 71
  • 90