views:

509

answers:

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: http://stackoverflow.com/questions/1295397/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?

+2  A: 

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: http://stackoverflow.com/questions/626057/is-it-possible-to-cache-post-methods-in-http

Cleiton
why it was downvoted? I dont see anything wrong with my answer.
Cleiton
-because you state it cannot be done with javascript. -because using POST instead of GET is not overly restful.
Zed
@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
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
@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
I just don't see you suggesting the same thing we were in your answer. I see you saying it cannot be done.
Zoidberg
@Zoidberg; So you are blind LOL, anyaway if you have time click on the link i've sent at the end of my answer.
Cleiton
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
OOoh i see it now...
Zoidberg
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
+4  A: 

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
LOL, mine is exactly the same as this one and I got down voted... NICE
Zoidberg
I got a downvote too... looks like someone just mass-downvoted all the answers.
Greg
So stupid... because our answers are good answers... reliable, will solve the problem.
Zoidberg
Very odd to downvote them all and leave no comment... such is life
Greg
Also, stop spraying me with ink, Zoidberg! Put on pants, Zoidberg!
Greg
WOOOB WOOOB WOOOB
Zoidberg
Sometimes people is very ungrateful. upvoted you to compensate ;-)
The Disintegrator
A: 

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.

Zoidberg
A: 

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
oh, well. when I started to write, there was no answers. Slow typer :-(
The Disintegrator
LOL, me too, I was about 10 seconds too late...
Zoidberg