2

I can't find a way to execute a script when a page is reached via the browser's BACK button or key command. (Background: I need to know if a page was opened using the browser's BACK button or key command. Then I could check for a stored sessionStorage varible and trigger some appropriate stuff).

If, for example, I put this into my page code

<script>
  $(document).ready(function() {
    alert("YES!");
    /* in the real situation: do something */
    })
</script>

, the alert will be displayed when I load the page via a link or by opening it directly by typing its URL into the address bar.

But this alert will not appear when I come to that page via the BACK button.

I tried the same using $(window).on("load", function() {... and $(window).on("navigate", function () {... - no success either...

EDIT / ADDITION:

I just realized that browsers behave differently in this respect: Chrome, Opera, IE and Firefox Windows will reload the page correctly (and trigger document.ready() and onload() events) , but Firefox Mac and Safari will not - they load the whole page from cache without triggering document.ready() or onload(). (I haven't tried mobile browsers yet)

So i searched for solutions avoiding the cached content, but what I've found and tried so far (which is a lot!) hasn't worked either...

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • possible duplicates: http://stackoverflow.com/questions/26650496/how-to-detect-if-a-user-has-go-to-a-page-by-using-the-browser-back-button http://stackoverflow.com/questions/2008806/how-to-detect-if-the-user-clicked-the-back-button – Zverev Evgeniy May 15 '16 at 21:27
  • @ZverevEugene this is *not* a duplicate: The question you quote doesn't even have an answer, just some later added workaround in the question, with a hidden form where the OP admits that he doesn't even understand it himself. – Johannes May 15 '16 at 21:39
  • Have a look at the second article then. Anyway, there are quite a few questions on exactly the same matter. I just mentioned the first two. – Zverev Evgeniy May 15 '16 at 21:53
  • @ZverevEugene and there is no clear answer to any of them... – Johannes May 15 '16 at 21:56
  • Have you considered solving that problem by setting some sort of session or cookie? Maybe it is more appropiate. – Leonel Atencio May 15 '16 at 22:50
  • @LeonelAtencio I have used localStorage and sessionStorage. But I can't find a way to read those values when using 'back'. But I am just having another idea that I'll try... – Johannes May 15 '16 at 23:03
  • @Johannes Ok, when you figure it out, try posting your solution and/or consider closing the question. – Leonel Atencio May 16 '16 at 03:01

1 Answers1

2

after reading lots of posts and trying the solutions in there and narrowing the thing down to a browser issue, I discovered this answer, which forces the page to be reloaded also in Safari (and apparently also Firefox/Mac):

https://stackoverflow.com/a/13123626/5641669

Essentially, this jQuery code reloads the page in its original state when coming there via the back button on Safari, which also allows to trigger any desired script on pageload:

$(window).bind("pageshow", function(event) {
    if (event.originalEvent.persisted) {
        window.location.reload() 
    }
});
Community
  • 1
  • 1
Johannes
  • 64,305
  • 18
  • 73
  • 130