views:

240

answers:

4

Is there a way to construct a link to a URL that forces the browser to ignore any cache it might have for that URL?

We have some websites that have recently changed after literally years of static content. Users click shortcuts, favorites, or type URL's to these sites and get old, cached content. I would like to have a page with a link that goes to the same URL but forces the browser to get the fresh content.

is that possible? Cross-browser?

+4  A: 
  1. Change all the links to the page with a nonsense query parameter: products.html?happy=days
  2. Change all the resources on the page to use another nonsense query parameter: style.css?the=fonz

You can do it in a systematic way using dates or timestamps or version numbers, or haphazardly using sitcoms, muppets, mountain ranges, and small mammals.

Tom Ritter
Do you know of a way to form a URL that forces shortcuts to be refreshed? For example, I see how www.abc.com/?happy=days would be fresh content but if I had a shortcut to www.abc.com would it be refreshed too?
n8wrl
Tom, are you saying that one would need to do both of these things or just one of them?
Clay Nichols
+1  A: 

Add a url parameter to the end of the href with a unique value, say new Date().getTime():

...somePage.html?time=123456789
happytime harry
A: 

Many people use a version query string parameter.

Check out the SO source

<script type="text/javascript" src="http://sstatic.net/so/js/master.js?v=4143"&gt;&lt;/script&gt;

Specifically notice the master.js?v=4143

Personally, I set this in my config file which is application wide, but I believe you can do this right in your source control if you need more granular automatic versioning.

John MacIntyre
A: 

When I need to have a complete refresh of the page, I simply add ?v= followed by a random string of character. When a browsers detect a query string after a page, they automatically bypass the cache (if the same query string is not present in the cached version), as the content of the page could change depending on the value of the query string.

Since a web page can be reloaded more that once by second, I use the current UNIX timestamp followed by a letter and a random number between 1 and 1000.

The chances that two pages get exactly the same query string are close to null.

xav0989