Search code examples
jqueryreloadback-buttonpageloadpage-refresh

Force page to load even when landing by navigating back (by back-button)


I'm quite desperate due to the following issue: How the hell :-) .. can I force my page to be reloaded everytime so lands on it? I have a input which is automatically filled with text from localStorage if this value is set. And I have links on my page to other pages. So (just an example) maybe the user clicks on pagexyz.com and then decides to navigate back to my site by clicking the backbutton. I tried a whole lot of things but am not able to force my piece of javascript to be executed.

The solution given here did not help me around.

These did not help.

So what else to do? I tried setting all cache-related meta tags - there must be a way to execute js when the page is called!?

So thankful for any kind of hint! hos


Solution

  • The problem is that by navigating back with the back button of the browser, the onload event of the page usually does not fire, neither will the JS code in the global scope be executed.

    One possible solution that is widely used for the so-called "hash history hack": Start an interval with setInterval() that checks every (let's say) 500 msec for a hash change and refresh the content according to the hash.

    On a usual content refresh you'll have to set the hash manually (window.location.hash="..."). And on initial page load (perhaps from a bookmark) you'll have to check the window.location.hash and load content accordingly.