1

In the following samples, the first page retrieves from localstorage then the second page sets localstorage. In Firefox and Safari (only) the value is not changed in the first page until I refresh the page. I do not need to do the explicit refresh in Edge, Chrome, Opera and IE. How can I get Firefox and Safari to process items updated in another page when a page is navigated back (returned) to?

Previous answers for similar problems say to disable the cache. I have tried to do that in many ways but it does not work for me. I tried the storage event and that does not work either, probably because the page is in history at the time. I can't find any event that occurs when the page is navigated back to. The focus event might work but it would likely be complicated and very vulnerable to problems.

Note that I want a solution that will work even if there are other pages of the web site in history. I want the pages in history to also refresh automatically when localstorage has been modified. So even a dialog or pop-up or whatever to modify localstorage would not work since it would not affect pages in the history.

The following is a sample of the first page:

<!DOCTYPE html>
<html>

    <head>
        <meta content="en-us" http-equiv="Content-Language" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <meta http-equiv="Cache-Control" content="no-store, must-revalidate" />
        <title>Update Browser</title>
        <script type="application/ecmascript">
            function Show() {
                var Stored = localStorage.getItem("StorageUpdateTest");
                var d = new Date(Stored);
                output.innerHTML = d.toLocaleTimeString();
            }
            window.addEventListener("storage", Show, false);
        </script>
    </head>

    <body onload="Show();">

    <h1>Browser</h1>

    <form method="post">
        <p id="output"></p>
    </form>

    <p><a href="StorageUpdate.html">Update</a></p>

    </body>

</html>

Note that I have a meta tag to disable the cache. I have tried other possibilities for disabling the cache too and none of them work. The following is a sample of the second page:

<!DOCTYPE html>
<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Storage Update</title>
    <script type="application/ecmascript">
        function Update() {
            var d = new Date(Date.now());
            localStorage.setItem("StorageUpdateTest", d);
            output.innerHTML = d.toLocaleTimeString();
        }
    </script>
</head>

<body>
<h1>Storage Update</h1>

<form method="post">
    <p id="output"></p>
    <input name="ButtonUpdate" type="button" value="Update" onclick="Update();">
</form>

</body>

</html>
Johannes
  • 64,305
  • 18
  • 73
  • 130
Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
  • Do you have to have reliance on the user being able to access the localStorage by hitting the 'back' button in their browser? If you did a redirect instead, it would work in every browser. If I remember correctly, Firefox automatically caches pages to visit with the back button, whereas Chrome and IE do not. You might need a `META REFRESH` in that regard. – Obsidian Age Mar 21 '17 at 21:31
  • To add to Obsidian Age, if using Back button explicitly is not *required* for logic, in Update() of second page at the end of the function you could use `window.location = first-page`. – Isaiah Mar 21 '17 at 21:39
  • @ObsidianAge, this is for something I am hoping many others can use so I want to keep it as simple as possible. I have tried many meta tag possibilities and I don't find anything that works. – Sam Hobbs Mar 21 '17 at 21:39
  • Have you tried the meta tags suggested in [this answer](http://stackoverflow.com/a/1341133/2341603)? There's also some **amazing** documentation (with samples) about preventing caching [in this answer](http://stackoverflow.com/a/2068407/2341603) :) – Obsidian Age Mar 21 '17 at 21:40
  • @Isaiah, this is a sample but in real use the first page can be any page in the web site. – Sam Hobbs Mar 21 '17 at 21:41
  • second page could have a URL parameter that designates the page to return to? Rather than explicitly using `window.location = first-page` it could be `window.location = myURLParam` where the parameter is parsed from something like `var query = window.location.search.substring(1); var vars = query.split("&"); var myURLParam = vars[0];` – Isaiah Mar 21 '17 at 21:46
  • @ObsidianAge, no, I did not previously try that **exact** combination but it does not work either. That answer looks a little suspicious. I did try many other combinations. I did see that other answer too and I did try the HTML4 option. Did you look at my sample? Did you see the meta tag? I did try using the other parts too but based on the documentation what I have seems to be most relevant if it was to work. – Sam Hobbs Mar 21 '17 at 21:52
  • @Isaiah, I might try that if nothing easier (less likely for someone **else** to make a mistake with) comes up. – Sam Hobbs Mar 21 '17 at 21:58
  • @Isaiah, do you mean as in [javascript - localStorage updates between pages; works in FF 39.0 & Chrome 44, but not in Safari 8.0](http://stackoverflow.com/questions/31905740/localstorage-updates-between-pages-works-in-ff-39-0-chrome-44-but-not-in-saf?rq=1)? – Sam Hobbs Mar 21 '17 at 23:14
  • That one suggested closing/reopening the window. using window.location would simply redirect after that Update() function executes, in effect giving the previous page (first-page) the chance to "refresh" after updates regardless of the browser. – Isaiah Mar 23 '17 at 01:09
  • @Isaiah, I have updated the question to explain that I need a solution that will work even if there are other pages of the web site in history. – Sam Hobbs Mar 23 '17 at 17:09

1 Answers1

0

I am attempting an answer to clarify what I was trying to explain through comments.

First, you cannot go though browser history and refresh the pages. Through JS you cannot access browser history for all browsers through some standard method, only with APIs provided by the browser, so therefore you cannot have any JS logic that will utilize the history in some custom way (and that will work across all browsers, the only functionality I can think of that you have access to is window.history, but that only mimics the back/forward button functionality).

Edit

In addition: It is not possible to "refresh pages in history". History is browser-specific functionality that is different for each because official rules are not well-defined for this behavior (this is also why not caching is also a no-go). Because page information does not persist (some of the page may in some browsers this is not guaranteed in others), you likely cannot handle this with only JS/CSS or any other client-side method (maybe cookies?).

/Edit

Correct me if I am wrong, but this is the desired flow:

User enters a page (page-a,page-b,page-c, ...) the page has data on it -> using a link from the page the user enters a new page called StorageUpdate.html -> the user uses an interface on StorageUpdate.html that updates data that you want reflected on the previous page (the page that sent the user to StorageUpdate.html, could be page-a, page-b, page-c, ...).

After updating data on StorageUpdate.html the page to return to is not a static page but needs to be dynamic (if user was in page-a before StorageUpdate.html then return to page-a, if user was in page-b before then return to page-b, ...).

For this I would recommend the following:

The following is a snippet of the first page (could be page-a, page-b, or whatever. Replace "thisPage.html" below with the name of the page the link is on -- this could be dynamically placed using JS and window.location if desired):

...
    <p>
         <a href="StorageUpdate.html?pageToReturnTo=thisPage.html">Update</a>
    </p>
...

Then the following is a snippet for StorageUpdate.html:

...
<script type="application/ecmascript">
    function Update() {
        var d = new Date(Date.now());
        localStorage.setItem("StorageUpdateTest", d);
        output.innerHTML = d.toLocaleTimeString();
        //here is the redirection after update logic takes place (could be in a function)
        var query = window.location.search.substring(1);//only get query vars from url
        var vars = query.split("&");//in case there are multiple params
        var pageToReturnTo = vars[0];//we only want the url param "pageToReturnTo" from above
        window.location = pageToReturnTo;//send user "back" dynamically
    }
</script>
....

Also, this does not need to be done for all pages in history because by simply having the user redirect themselves to other pages they will get a "page refresh" and have the most recent data.

Edit

You may try a change in flow altogether! For example ajax calls that avoid the "history issue" completely! Use async requests to handle update then refresh the current page. For this to be taken into effect across multiple pages, you would have to combine the pages/data such that they can be refreshed collectively.

/Edit

In case this is not what you are looking for you can force a page refresh on any page with window.location = window.location in a timing loop. Again you would want some exterior variable (could be a get variable because it would persist across page refreshes) that prevents the refresh when you don't want it to happen.

Edit

Finally, this is likely your best bet. A timed loop would require some information provided here. I believe a timed loop would be your best bet as it would force a page refresh, the tricky part is determining when to force it. In the same link you will also find some JS form of detecting forward/back button press events that is becoming more supported.

/Edit

Community
  • 1
  • 1
Isaiah
  • 484
  • 5
  • 16
  • Note that I said "I want the pages in history to also refresh automatically". Note that "pages" is plural. Yes, of course I know that we have very limited access to the history. And if we do update the page that links to "Storage Update" then we still have the problem that when the back button is pressed again then the page in history needs to also refresh. I can figure out how to use a timer if that is necessary but I want something internal to the page that needs to refresh. It might not be possible but if it is then it is specific to Firefox and/or Safari. – Sam Hobbs Mar 23 '17 at 19:43