myNewAccount
myNewAccount

Reputation: 640

Is there a way to force window.location.reload() to reload the current URL only one time?

I'm testing window.location.reload() to see if I can use it to ensure that a client dashboard has the latest value if the client arrives at the dashboard from a forward or back button click.

Unfortunately the default behavior of this method in Chrome and possibly other browsers causes the browser to reload a seemingly endless number of times.

Is there a way to force window.location.reload() to reload the current URL only one time?

If I can get that to work maybe I can use window.history or some other method to determine if the client arrived at the page from a forward or back button click. Still researching that.

Upvotes: 0

Views: 140

Answers (1)

Aaron McGuire
Aaron McGuire

Reputation: 1255

One approach would be to set a sessionStorage Variable.

if(sessionStorage.getItem('loaded') === null) {
  sessionStorage.setItem('loaded', true);
  window.location.reload(); 
}

Upvotes: 2

Related Questions