0

I've run into an interesting issue and I'm not sure how to solve it. I maintain a hidden value on my homepage that tracks the events loaded on the page. The event position gets updated after the page loads. When navigating away from the page and then clicking the browsers back button the previous event number is seen. If I click a link and load the home page directly the event position gets reset (as expected)

My HTML:

    <input type="hidden" id="event_num" value="0">

My javascript call to get/set the event_num value:

 function getRecentEvents() {

    var event_pos = $('#event_num').val();
    //console.log("POS: " + event_pos);

    $.getJSON("functions/get_events.php", { f: 'get_events', event_pos: event_pos, limit: limit}, function(data) {

        if (data.events.length > 0) {

            // set the new value        
            $('#event_num').val(data.events_pos);
        }
});

I would expect the hidden field to be 0 every time the page is loaded regardless if the user clicked a link or hit the back button on the browser.

Any idea how I might fix this or is this a known issue?

Paul
  • 11,671
  • 32
  • 91
  • 143
  • You may try searching for answers to questions about how to detect that the back button has been pressed. (http://stackoverflow.com/questions/6359327/detect-back-button-click-in-browser) You may have to write a function that resets your events when the page is revisited via back button. – jwatts1980 Aug 30 '13 at 19:40

2 Answers2

1

The page is not loaded again if you click the "back" button, the cached page content is shown. That is, the page as the state of it was the last time you visited it. You could reset the event_num counter on $(document).ready or on a document unload event, then you will get the expected behaviour.

Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55
0

This is an old question and the accepted answer is not correct, not at least by today's standards. @Erik A. Brandstadmoen answer regarding the Back-forward cache is correct, however, you should not use the unload event as this will most likely break the Back-Forward cache. Instead you should be using the pageshow / pagehide events. See this web.dev article that explains the Back-Forward cache: https://web.dev/bfcache

rhys_stubbs
  • 538
  • 6
  • 16