2
votes

Is there any way to know if the loaded page comes from a back button call?

This is the path:

  1. index.html (link to page1 in the menu)
  2. page1.html (loads content from ajax with link of page2)
  3. page2.html (press BACK button)
  4. page1.html (DETECTS BACK BUTTON PRESSED FROM PAGE2, THEN DO SOMETHING)

Page 2 can be loaded from pageX so it is not an option to use onbeforeunload or something like that to handle back/forward button in page2 to redirect to page1, page2's back button can redirect to pageX instead of page1.

I need to put the code in page1

Is it possible?

1
not really. especially if the previous page is loaded from the browser's cache. Going "back" to a previous page wouldn't change that previous page's referer. e.g. you go a->b->c->b, then page b's referer will STILL be page a, not c.Marc B
Why do you want to know this? (Maybe it's possible to archive without knowing if a load event was caused by back navigation.) Note when using a backend technology you might track the progress of the user in a session and thus be able to determine back navigation.try-catch-finally

1 Answers

2
votes

Well, there isn't a way to look for that directly as best I know. You could add onbeforeunload to all pages and have them set a cookie of the current URL before transition. Then you could check that cookie on page1 to see if the last URL was page2.