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?

Paramasivan
  • 781
  • 3
  • 12
  • 27
  • 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. – errand Dec 08 '14 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 – A. Wolff Dec 08 '14 at 12:58
  • If you reload your page the `$(document).ready` is loaded again. – Der Vampyr Dec 08 '14 at 12:59
  • I want to reload the page only one time when the user goes to that page. – Paramasivan Dec 08 '14 at 13:03
  • @Paramasivan **AND WHY** would you need that??? – A. Wolff Dec 08 '14 at 13:04
  • I have made some changes to html form action script. Users get old action file. – Paramasivan Dec 08 '14 at 13:07
  • @Paramasivan You'd have better to ask question regarding this ↑ ↑ ↑ instead of the workaround you think would fix it: http://mywiki.wooledge.org/XyProblem – A. Wolff Dec 08 '14 at 13:14

4 Answers4

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();
    }
});
Mooseman
  • 18,763
  • 14
  • 70
  • 93
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;
    }
});
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • 1
    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! – CodingIntrigue Dec 08 '14 at 13:04
  • @RGraham Yes, it's a little bit more concise then `getItem`. You can also use bracket notation like like with ordinary object. – dfsq Dec 08 '14 at 13:06
  • 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:*** http://stackoverflow.com/a/15439059/1414562 – A. Wolff Dec 08 '14 at 13:10
  • @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 Dec 08 '14 at 13:13
  • @dfsq Ya it doesn't really matter – A. Wolff Dec 08 '14 at 13:15
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();
}
Sam Greenhalgh
  • 5,952
  • 21
  • 37
-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.

Connor Wyatt
  • 166
  • 11