views:

1530

answers:

7

Is there a way I can put some code on my page so when someone visits a site, it clears the browser cache, so they can view the changes?

Languages used: ASP.NET, VB.NET, and of course html, css, and jquery.

A: 

Look into the cache-control and the expires META Tag.

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT">

Another common practices is to append constantly-changing strings to the end of the requested files. For instance:

<script type="text/javascript" src="main.js?v=12392823"></script>

Jonathan Sampson
This won't help much in the case that it's already cached—since its cached, the server won't be queried, and thus can't responsd with no-cache. Also, that meta tag really shouldn't be used, as the note says, it'll break with web caches.
derobert
+1 what derobert said. Always better to use HTTP headers to suggest cache policy to clients and web caches but even that doesn't work to force a cache reload.
fsb
+3  A: 

If it's to view css or js changes one way is to append _versionNo to the css/js file for each release. E.g.

script_1.0.css script_1.1.css script_1.2.css etc.

You can check out this link to see how it could work.

Fermin
This is a fairly good solution, and can even be automated by your build system (and should be). Stackoverflow, for example, uses this approach.
derobert
+3  A: 

Not as such. One method is to send the appropriate headers when delivering content to force the browser to reload:

Making sure a web page is not cached, across all browsers.

If your search for "cache header" or something similar here on SO, you'll find ASP.NET specific examples.

Another, less clean but sometimes only way if you can't control the headers on server side, is adding a random GET parameter to the resource that is being called:

myimage.gif?random=1923849839
Pekka
It's really better to properly version the files. This is a pretty big waste of bandwidth, and, probably more importantly, slows your site down a lot.
derobert
That really depends on the situation, doesn't it? If you are programming a CMS and need to make sure all changed resources are properly updated, there sometimes is no way around one of these two options.
Pekka
A: 

Do you want to clear the cache, or just make sure your current (changed?) page is not cached?

If the latter, it should be as simple as

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
Tim Crone
A: 

Here is the MDSN page on setting caching in ASP.NET.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.Cache.SetValidUntilExpires(False)
Response.Cache.VaryByParams("Category") = True

If Response.Cache.VaryByParams("Category") Then
   '...
End If
Dave Swersky
A: 

In addition to setting Cache-control: no-cache, you should also set the Expires header to -1 if you would like the local copy to be refreshed each time (some versions of IE seem to require this).

See http://stackoverflow.com/questions/1917586/http-cache-check-with-the-server-always-sending-if-modified-since/1917827#1917827

danben
A: 

This is a quite obvious duplicate question, but this should do the trick:

<meta http-equiv="pragma" content="no-cache" />

Also see http://stackoverflow.com/questions/126772/how-to-force-a-web-browser-not-to-cache-images

S Pangborn