0

I'm trying to reload a page using Javascript in my php code.

The page must be a fresh copy from the server.

I've tried the following:

1) Inserting a reload code in the code

echo "<script>
location.reload(true);
</script>";

Problem: this code keeps reloading the page.

2) Going back in the parent page:

echo "<script>window.location.href = document.referrer;</script>";
or
window.location.href = window.location.href;

Problem: this code does not get a fresh copy from the server, resulting in a outdated page.

Is there a way that i can reload (getting a fresh copy from the server) that runs only once, or get a parent, but updated page?

What am i missing here?

Pedro Ivo
  • 39
  • 7
  • 2
    Try adding a condition around the reload. When do you want the page to reload? Maybe in response to some event or application state? If you have no condition, that's an infinite loop. – ggorlen Apr 17 '20 at 01:18
  • Does this answer your question? [JavaScript hard refresh of current page](https://stackoverflow.com/questions/2099201/javascript-hard-refresh-of-current-page) – ggorlen Apr 17 '20 at 01:19
  • @ggorlen (second comment), nope, because the reload(true) causes a infinite loop. – Pedro Ivo Apr 17 '20 at 13:19
  • @ggorlen how can i loose the infinite loop? The condition is just to reload once. – Pedro Ivo Apr 17 '20 at 13:29
  • Multiple ways to do it, although this practice seems pretty atypical. You could set a flag in [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), set a cookie or make a remote request to a server database. It's a design call that's up to you. – ggorlen Apr 17 '20 at 14:34

0 Answers0