0

When updating the content of a text file, it does not update. Instead, I have to delete all cache and whatnot in my Google Chrome to make it refresh it. However, it does work when using a Apache server (local host).

I tried adding a: <meta http-equiv="expires" content="Mon, 26 Jul 1997 05:00:00 GMT"/> <meta http-equiv="pragma" content="no-cache" />, but the problem still occurs.

function MakeRequest()
{
  var xmlHttp = getXMLHttp(); // just a basic XML request
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }
  xmlHttp.open("GET", "counter.txt", true); 
  xmlHttp.send(null);
}

And I have another JavaScript just printing this in a div: (timed to run with some delay)

document.getElementById('counterHolder').innerHTML = response;

Click here if you need to see the webpage live.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
user1431627
  • 815
  • 3
  • 13
  • 30
  • Have you checked your javascript console? I keep getting 'number is not defined'; what are you doing with the variable 'number'? – Daedalus Jul 03 '12 at 23:38
  • Checked now - it was a small pice of code that I was not using anymore. – user1431627 Jul 03 '12 at 23:43
  • So why is it still there? I've noticed that sometimes, if there is an error in a function, it stops executing the function altogether; if it is no longer being used, can you please remove it? – Daedalus Jul 03 '12 at 23:46
  • Just forgot to remove it. Thank you for noticing the extra unnessesary code. – user1431627 Jul 04 '12 at 09:55

3 Answers3

2

This appears to be a caching problem. I can't exactly describe why it happens the way it does, but in my experience, adding a random string of characters to the end of your url in your get request should work. I would suggest something like below:

xmlHttp.open("GET", "counter.txt?" + Math.random(), true);
Daedalus
  • 7,586
  • 5
  • 36
  • 61
1

I believe something similar to this post would help you out. Its aimed at linking to a file with would be seen as a fresh link every time the page is reloaded, however, same would apply in your situation:

HTML Link that bypasses Cache

Community
  • 1
  • 1
Andre
  • 11
  • 1
  • 4
  • +1 basically, the linked post says you just need to add a timestamp as a query string parameter. so, something like: `url = url + "?q=" + new Date().getTime();`. I've done this in the past. I'm assuming there is a way you can configure your cache settings to solve the problem, but I think this is much quicker and less obtrusive. – lbstr Jul 04 '12 at 00:00
0

I recommend using jQuery, or another decent JavaScript library. If you do, then check that there's a cache option in jQuery.

Trying to fix your problem as it is, try this..

function MakeRequest()
{
  var xmlHttp = getXMLHttp(); // just a basic XML request
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }
  var timestamp_since_epoch = new Date().getTime();
  xmlHttp.open("GET", "counter.txt?"+timestamp_since_epoch, true); 
  xmlHttp.send(null);
}

What the above does, is generate a timestamp since the epoch, and add it as parameter to the AJAX Call.

André Catita
  • 1,313
  • 16
  • 19