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...

7
  • possible duplicates: stackoverflow.com/questions/26650496/… stackoverflow.com/questions/2008806/… Commented May 15, 2016 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. Commented May 15, 2016 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. Commented May 15, 2016 at 21:53
  • @ZverevEugene and there is no clear answer to any of them... Commented May 15, 2016 at 21:56
  • Have you considered solving that problem by setting some sort of session or cookie? Maybe it is more appropiate. Commented May 15, 2016 at 22:50

1 Answer 1

1

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() 
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.