0

I want to reload a page only 1 time when the user goes to that page.

I am using the following script:

$(document).ready(function() {
    for (i = 1 ; i <= 1 ; i++) {
    location.reload();
    }
});

But it keeps on reloading the page. Any error with the for loop?

7
  • yeah - the error is, that if you reload yourself, the script also gets reloaded and therefore restarts! you have to wrap something around your page, that loads the content. Commented Dec 8, 2014 at 12:58
  • 1
    Check for any persistent data client side as cookie or local/session storage. That's said, sounds really like a xy problem. Why would you need to reload page and especially in a for loop??? Doesn't make any sense Commented Dec 8, 2014 at 12:58
  • If you reload your page the $(document).ready is loaded again. Commented Dec 8, 2014 at 12:59
  • I want to reload the page only one time when the user goes to that page. Commented Dec 8, 2014 at 13:03
  • @Paramasivan AND WHY would you need that??? Commented Dec 8, 2014 at 13:04

4 Answers 4

3

JavaScript resets every time the page is loaded. Once the page is reloaded, your for loop is history. Here's an alternative using HTML5 Local Storage:

$(document).ready(function() {
    if(localStorage.getItem('reload') != false)
        localStorage.setItem('reload', false);
        location.reload();
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

This will only work the first time you visit the page. If you go back to the site later then it won't reload the page.
@Archer The OP hasn't explained it very well, but I think that is the requirement
@Archer If the OP is using a login system, they could .removeItem a sessionStorage on logout. This would most likely be the best solution.
2

You need somehow make page remember that it's has already been loaded before. I would use localStorage/sessionStorage:

$(document).ready(function() {
    if (!localStorage.reloaded) {
        location.reload();
        localStorage.reloaded = true;
    }
});

5 Comments

Someone's being very harsh with their downvotes today. Btw, I did not realise we could use dot/bracket notation with localStorage. I'll consider that my something new for today, thanks!
@RGraham Yes, it's a little bit more concise then getItem. You can also use bracket notation like like with ordinary object.
You'd have better to set localStorage property before calling reload() method for better cross browser support. As a side note regarding setting property of localstorage vs using relevant method: stackoverflow.com/a/15439059/1414562
@A.Wolff Sure, I'm aware about differences between getItem and property access. Just in case of checks for falsy values it doesn't matter.
@dfsq Ya it doesn't really matter
1

As others have mentioned, you'll need some mechanism of maintaining some kind of flag to indicate that the page has been reloaded that persists after reloading.

Common solutions to this would be cookies, url fragments, local storage.

Cookie

if(!(/\breloaded=1/.test(document.cookie))){
    document.cookie = 'reloaded=1';
    location.reload();
}

URL fragment

if(location.hash != '#reloaded'){
    location.hash = '#reloaded';
    location.reload();
}

Local Storage

if (localStorage.getItem('reloaded') != false) {
    localStorage.setItem('reloaded', false);
    location.reload();
}

Comments

-1

You can set a cookie to be set using jquery when you first load the page and then use an if statement to run the reload if the page hasn't already been reloaded (which will be defined in the cookie) and delete the cookie after.

Comments

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.