farinspace
farinspace

Reputation: 8801

How can I invalidate a page in the browser cache from the client side?

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?

Upvotes: 4

Views: 5824

Answers (4)

Zoidberg
Zoidberg

Reputation: 10323

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.

Upvotes: 3

Cleiton
Cleiton

Reputation: 18113

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?

Upvotes: 6

The Disintegrator
The Disintegrator

Reputation: 4187

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.

Upvotes: 1

Greg
Greg

Reputation: 321698

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();

Upvotes: 7

Related Questions