0

I want to reload my page after all my code is loaded without using a button.

I used: location.reload();

The problem is that it repeatedly reloads my page all time.

6
  • 3
    Why? Smells like an XY problem. Commented Dec 17, 2012 at 20:39
  • Even if you do it after your page has finished loading it will still reload in a loop .. at what point do you want it to stop reloading? Commented Dec 17, 2012 at 20:39
  • 2
    This is one of those questions where we really would like to know what you're trying to accomplish, so we can suggest a solution. Commented Dec 17, 2012 at 20:40
  • 1
    @Explosion Pills : exactly like a loop, I want to execute this function just one time after my page loaded totally. Commented Dec 17, 2012 at 20:41
  • 1
    @SlimMils: Why does it reload the page? What are you trying to do? Commented Dec 17, 2012 at 20:44

3 Answers 3

7

Your window.reload() is getting hit every time the page is reloaded. Do something like this:

if(window.location.href.indexOf('?reloaded') === -1) window.location.href = window.location.href + "?reloaded=true";

This assumes that the page isn't using any other url parameters.

EDIT

As Matt Ball pointed out, using localStorage or cookies won't be restrictive to a single tab or window, but as Jackson Sandland pointed out in the comments, my above-given solution will alter the browser history. After doing some digging I came up with another, admittedly "hacky" but seemingly effective solution: Using window.name instead of a flag in the URL:

if(window.name !== "refreshed") {
    window.name = "refreshed";
    window.location.refresh();
}

If you'd like to test this out, paste the following in an HTML file and load it in your browser:

<script>
window.name = prompt("Window name", window.name);
</script>

Enter a new name, then refresh the page. You'll see that on refresh the new name is displayed. Similarly, if you then open a second tab with the same file, you can set the name for that tab independently of the other.

Sign up to request clarification or add additional context in comments.

3 Comments

Not ideal. You are appending the URL. This effects browser navigation.
I agree, @Jackson_Sandland. Happily, in my many years of development I've seldom seen a problem that couldn't be solved in a better way than doing something like the OP is requesting. If you've ended up here, perhaps there is a better solution (than refreshing) to your problem as well.
@Jackson_Sandland I did some digging and found another hack. I've updated my answer.
3

Your best bet is to flag somewhere that it has been reloaded once. Using cookies or localStorage as said by Matt, if you need per-tab you can change the location.hash

Maybe you can help us understand WHY you want to reload, there could be another way.

Comments

2

You need to store some sort of state outside of the page's JavaScript that will indicate whether or not to refresh. One way is to use localStorage, or a cookie, but neither of these will work per-tab or per-window.

1 Comment

A more detailed answer would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.