1

So my javascript files are being cached. Originally I just had them in a script tag like so:

<script type='text/javascript' src='js/example.js'></script>

Now my understanding is that it is perfectly fine for browsers to cache GET requests.

Since my application is in development still I can't allow this for now.

My second understanding about caching is that if the GET query strings are different then it wouldn't see them as the same therefore not caching them, with this in mind I added this to my pages instead:

<script type="text/javascript">
    window.onload = function () {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = "js/example.js?timestamp=" + new Date().getTime();
        document.body.appendChild(s);
    }
</script>

This loads the file fine coming up with something like:

<script type="text/javascript" src="js/example.js?timestamp=1369194644133"></script>

But somehow this is still being cached.

I also have cache disabled on my browser (Google Chrome) but its still happening.

To serve the files I am using IIS 7.5 and I have also disabled cache for the site on that as well.

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • 1
    How do you know it's being cached? – Blender May 22 '13 at 04:03
  • Because I checked the file by going to the location and compared it to the file that its loading. – FabianCook May 22 '13 at 04:04
  • 2
    I would monitor the site using fiddler (or similar) to be sure the requests for your JS files don't go to your server – TGH May 22 '13 at 04:05
  • 1
    Also you check the response headers using the network tab of your browser developer toolbar – Arun P Johny May 22 '13 at 04:10
  • I'm sure it was just a typo, but in your code, you have `s.src = "js/example?timestamp=" + new Date().getTime();` - where's the **.js** extension? – Ian May 22 '13 at 04:10
  • Was a typo in the example xD. I didn't originally have cache on IIS and I think that along with the query string helped it out. Don't think its caching any more. – FabianCook May 22 '13 at 04:13
  • Browsers will reload a file if the webserver includes a proper modification date in the header. Some FTP clients like to preserve modification dates on files, leading to caching problems. It would be nice to see some actual request and response header traffic in the question. – Cobra_Fast May 22 '13 at 04:37

1 Answers1

-1

try to use Math.random()

 xhr.open('GET', 'php/response.php?page=' + this.page + '&' + Math.random(), true);
socm_
  • 783
  • 11
  • 28
  • 1
    This isn't different to what OP already has at all. It's just another way to get a different number every reload... – Cobra_Fast May 22 '13 at 04:40