4

I need the client (using javascript) to invalidate a page it has and essentially fetch a new version?

I thought I could do it all with headers: Invalidating cached content, If-Modified Headers?

If there NO way to have the browser refresh its current cached version, with out making a new request (via a new URL) ... so that the same original URL request could be used to see the updated content?

Community
  • 1
  • 1
farinspace
  • 8,422
  • 6
  • 33
  • 46

4 Answers4

7

If you want to reload the current page you can do:

location.reload(true);

Otherwise the "traditional" way is to add a random querystring onto the end

'...?rnd=' + Math.random();
Greg
  • 316,276
  • 54
  • 369
  • 333
6

You can't do that with javascript, to solve your problem or use method POST instead of GET or use nocache random parameter trick:

If you want more information, see: Is it possible to cache POST methods in HTTP?

Community
  • 1
  • 1
Cleiton
  • 17,663
  • 13
  • 46
  • 59
  • why it was downvoted? I dont see anything wrong with my answer. – Cleiton Aug 19 '09 at 19:01
  • -because you state it cannot be done with javascript. -because using POST instead of GET is not overly restful. – Zed Aug 19 '09 at 19:17
  • @Zed, thanks for your comment. But at least I know It is impossible "to invalidate a page cache" using only javascript, and when we use POST method we force IE to get lastest version, the corresponding RFC 2616 states explicitly in section 13 (Caching in HTTP) that POST requests should not be cached. – Cleiton Aug 19 '09 at 19:26
  • I don't think thats what the asker meant, he just wanted a way to ensure that he would NOT get the cached version of the page when requested on the client side. If you must know, YUI uses the exact method both Greg and I suggested to avoid browser cache. Sometimes this is neccessary if your pages aren't generated by serverside scripts. (like json, html, css etc...) – Zoidberg Aug 19 '09 at 19:34
  • @Zoidberg, sure I KNOW the method that YUI(and others js frameworks do)! I was the first in this question to suggest that and I just got curious to know why even every qeustion below mine is the same i was the only that got two downvotes LOL – Cleiton Aug 19 '09 at 19:43
  • I just don't see you suggesting the same thing we were in your answer. I see you saying it cannot be done. – Zoidberg Aug 19 '09 at 19:45
  • Still don't see it, not in your link or in your answer... maybe I need new glasses... or maybe I miss my old ones.... – Zoidberg Aug 19 '09 at 19:55
  • I actually find this to be the best answer so far ... I know about the random number trick, but what im trying to find out is after a page is cached, i want to refresh the new content on demand, i do not want to generate a new random number all the time, i want to refresh just the once and have subsequent requests to the page pull the new content refreshed – farinspace Aug 19 '09 at 21:03
3

When you reference the page, add a random variable on to the end. For instance:

document.location.href = 'mypage.html?randomVar=454068934';

That will ensure a non cached version. I recommend using javascript generated guids.

Simon East
  • 55,742
  • 17
  • 139
  • 133
Zoidberg
  • 10,137
  • 2
  • 31
  • 53
1

what I did is pass a random parameter in the url. ie if I need to fetch products.php I call it with products.php?rand=23443545. This way the cache doesn't interfere.

The Disintegrator
  • 4,147
  • 9
  • 35
  • 43