1

I'm searching for a simple code to put in the body(<body onload="...">) that automatically reload the page only once.
I found some ways like using location.reload() as in the both topics here: stackoverflow1 and stackoverflow2
but they aren't helpful because it autoload the page everytime it loads(not once), what I want is to reload the page only once on load.

Community
  • 1
  • 1
user3079620
  • 13
  • 1
  • 4
  • You can use `sessionStorage` or `localStorage` – tewathia Dec 08 '13 at 10:46
  • 1
    this sounds like a really weird way to solve your problem tell us more – worenga Dec 08 '13 at 10:46
  • Well I have listed an item on ebay with codes in the description and on loading the page my code gets inline with ebay code, the only way to solve this problem is by refreshing the page. I contacted ebay and they told me that it is something in the cache, do you have any better way to solve this from the cache? do you want the link of my listing? – user3079620 Dec 08 '13 at 10:50
  • this has been asked and answered many times: http://stackoverflow.com/questions/20213528/refresh-the-page-after-first-load-but-not-if-i-refresh-it-again – Rudy Dec 08 '13 at 14:49
  • Here on SO it is nice to accept an answer. I see here that you finally used my answer: http://stackoverflow.com/questions/20453335/calling-jquery-function-in-the-body I want to reach 1000 rep, THX – Rudy Dec 08 '13 at 21:24

3 Answers3

1

Use cookies or hash tags to store page state. This is the only possible way. By the way, why you need this? Perhaps there is a better way to achieve this?

try this:

body onload='if(document.cookie.indexOf("mycookie") == -1){document.cookie="mycookie=1";location.reload();};'
Bohdan Yurov
  • 365
  • 5
  • 9
  • Well I have listed an item on ebay with codes in the description and on loading the page my code gets inline with ebay code, the only way to solve this problem is by refreshing the page. I contacted ebay and they told me that it is something in the cache, do you have any better way to solve this from the cache? do you want the link of my listing? – user3079620 Dec 08 '13 at 10:48
  • can you reply please? – user3079620 Dec 08 '13 at 10:55
  • Could you make example page? – Bohdan Yurov Dec 08 '13 at 11:00
  • my code works well but it doesn't work great on ebay until you refresh the page, here's my listing: http://www.ebay.com/itm/321266212544?var=&ssPageName=STRK:MESELX:IT&_trksid=p3984.m1555.l2649 try to open the page from different browsers (not just google chrome) and reload the page – user3079620 Dec 08 '13 at 11:05
  • I'm not familiar with ebay, sorry. But as about your question - just set a cookie. – Bohdan Yurov Dec 08 '13 at 11:07
  • Can you please tell me how to set a cookie? I don't understand these things – user3079620 Dec 08 '13 at 11:09
  • Also can you please read this message about my problem so you may help me?: **When your first go to the web page, it takes a lot of time to load, that is because it is saving the page in something called your cache (The "cache" is where your computer stores images and web pages you've visited so they're easier to access).** - complete in the next comment – user3079620 Dec 08 '13 at 11:11
  • **When you next visit the page, it loads the stuff in your cache. If something is changed on that web page, your computer doesn't care until you refresh the page. It wipes out the cache, and re-loads the page with the new content. That's why if you don't refresh pages, you get the old stuff. Refreshing the web page updates the cache with new set of information or values and the latest information is viewed.** – user3079620 Dec 08 '13 at 11:12
  • try this: body onload='if(document.cookie.indexOf("mycookie") == -1){document.cookie="mycookie=1";location.reload();};' – Bohdan Yurov Dec 08 '13 at 11:19
  • it reload unlimited times :S :S :S – user3079620 Dec 08 '13 at 11:31
  • I just checked it on my local server - works like a charm. Perhaps, your have not removed old code. – Bohdan Yurov Dec 08 '13 at 11:33
  • I tested your code in a simple page, just put the code in a new html page and it reload unlimited times, here's the code: http://jsfiddle.net/84Qwz/ – user3079620 Dec 08 '13 at 11:39
1

Add this snippet in a script tag to your page.

$(function(){ // jQuery dom ready event
    if (window.location.href.toLowerCase().indexOf("loaded") < 0) {
        window.location = window.location.href + '?loaded=1'
    }
});
Rudy
  • 2,323
  • 1
  • 21
  • 23
  • The code works well but it adds "?loaded=1" to the url, can't I remove that? – user3079620 Dec 08 '13 at 11:04
  • can you please reply? I think your code is the best without the additional "?loaded=1" to the url – user3079620 Dec 08 '13 at 11:07
  • You can remove the additional string with `window.location.href = window.location.href.split('?loaded=1')[0]` – tewathia Dec 08 '13 at 11:15
  • Unrelated, but you might want to wait a little bit longer before you ask people to "please reply" - two minutes is not exactly enough time to get back to replying to comments. – Matthew Dec 08 '13 at 11:17
  • it reload unlimited times :/ – user3079620 Dec 08 '13 at 11:35
  • You can't remove that url param because if you do, the page will not stop refreshing, but you can change it to something with less meaning. – Rudy Dec 08 '13 at 13:12
0

Put this in your onload

function checkReloadCount() {
   if(localStorage.getItem('isLoaded') !== 'yes') {
      localStorage.setItem('isLoaded', 'yes');
      //put reload script here
   }
} 

DEMO

tewathia
  • 6,890
  • 3
  • 22
  • 27
  • it's not working for me.. should I add `location.reload()` in place of `//put reload script here`? that's what I did – user3079620 Dec 08 '13 at 10:54