0

I know, this question looks quite the same as those asked and reasked regularly, as for example here:
How to force reloading a page when using browser back button?

I need to know if the user has...
. navigated to current page, or reloaded current page (in both cases the page has been normally loaded, I need no further specific action)
. or has arrived on current page using history back or forward button (in these cases, the page is not loaded, just anyhow taken out of browser-cache, and I need to take further actions -the simplest being just reloading the page, window.location.reload(); or equivalent).

I tried the solutions exposed here: https://stackoverflow.com/a/43043658/3872061 and here: https://stackoverflow.com/a/56851042/3872061 as well as in several other places, within Stackoverflow or not.

It works well for Firefox (105.0.1), for Edge (105.0.1343.50), but I can't get it to work with Chrome (105.0.5195.127).

Here the simplest test I could imagine.

page1.html :

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script>
    $(window).on("pageshow", function(e){

      $("#type").text(window.performance.getEntriesByType("navigation")[0].type);
      let historyTraversal = event.persisted
        || ( typeof window.performance != "undefined" && window.performance.getEntriesByType("navigation")[0].type === "back_forward" );
      if ( historyTraversal ) { // Handle page restore.
        $("#backOrNot").text('User just came back using the history "back" button');
        //window.location.reload(); // this is actually the targetted action: reload page
      }
      else {
        $("#backOrNot").text('User loaded the page');
      }

    });
  </script>
</head>
<body>
  <p id="backOrNot"></p>
  <p>( window.performance.getEntriesByType("navigation")[0].type = <span id="type"></span> )</p>
  <p><a href="page2.html">Just a link from where you can click the history back button</a></p>
</body>
</html>

page2.html :

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
</head>
<body>
  <p>
    From here, try to:<br>
    1. Navigate to <a href="page1.html">page1.html</a><br>
    2. Go back to page1.html using the history back-button
  </p>
</body>
</html>

With both FF and Edge, when you come back to page1 using the history, the "back_forward" information is available.
With Chrome, you just get the status ("navigate" or "reload") which the page had on the initial load,as if you had never navigated away then returned using the back button.
What have I missed? What am I doing wrong here?

fpierrat
  • 739
  • 7
  • 25
  • Does this answer your question? [How to Detect Browser Back Button event - Cross Browser](https://stackoverflow.com/questions/25806608/how-to-detect-browser-back-button-event-cross-browser) – RKataria Sep 27 '22 at 13:19
  • I'm not in a single page context, but on different pages, server-side built when http requested, according to current state of DB data. Different URLs, different PHP scripts, not just a different # on same page. I'll dig all answers there still,but at first quick reading it's not the same issue. – fpierrat Sep 27 '22 at 13:29
  • Can I challenge the need -- WHY do you need to know that? Just making sure this isn't an XY question. – Chris Baker Sep 27 '22 at 14:19
  • Example: list of items "itemsList.php". User clicks on an item's link, let's say "My Itm", to view details. This link opens "item.php?id=foo". There he makes a few changes (ajax driven form), for instance corrects typo, "My Itm" ==> "My Item". Ajax posts occur saving changes into DB. Then he uses the back button to come back on the list. Because of "back", browser shows unmodified list from cache instead of loading "itemsList.php" from server. Still "My Itm" instead of corrected "My Item". User needs to press F5 to see his changes. – fpierrat Sep 27 '22 at 15:40

1 Answers1

0

Your page will automatically reload, if you send off page headers to not allow caching of that itemsList.php page. My test code -- see answer here -- should help you understand the concept. I am still trying to find a better way to solve that problem differently. In my case, I am firing off a "page-is-now-loading-spinner" that is not resetting itself to hidden, if the back button (go back one page) is presses.

MeSo2
  • 450
  • 1
  • 7
  • 18