tags:

views:

152

answers:

5

Hi,

I found out that some websites use css tag like style.css?ver=1. What is this??

What is purpose of ?ver=1 How do I do it in code?

+12  A: 

To avoid caching of CSS.
If the website updates their CSS they update the ver to a higher number, therefore browser is forced to get a new file and not use cached previous version.
Otherwise a browser may get a new HTML code and old CSS and some elements of the website may look broken.

Michal M
as an added note, you can add the timestamp of the request instead of a version number and completely bypass caching of the stylesheet altogether. This increases processing, but reduces code maintenance.
Jim Schubert
Agree with Jim, I use file's timestamp personally and it is much easier to use.
Michal M
+4  A: 

The purpose of the ?ver=1 is to parameterize the css file, so when they publish a new style.css file they up the version and it forces the client to download the new file, instead of pulling from the cached version.

Joseph
+2  A: 

I think that ?ver=1 is for the version no. of the web app. Every time a new build is created, the app can update the ver to the new version. This is so that the browser will load the new css file and not use the cached one (both use different file names).

YOu can refer to this site: http://www.knowlegezone.com/pages/document.aspx?item=36

devpl
+4  A: 

Adding '?ver=1' makes the HTTP request look like a GET query with parameters, and well-behaved browsers (and proxies) will refuse to cache parameterized queries. Of course well-behaved browsers (and proxies) should also pay attention to the 'Cache-control: no-cache', 'Expires', 'Last-Modified', and 'ETag' response headers (all of which were added to HTTP to specify correct caching behavior).

The '?ver=1' method is an expensive way to force behavior when the site developer doesn't know how (or is too lazy) to implement the correct response headers. In particular, it means that every page request is going to force requesting that CSS file, even though, in practice, CSS files change rarely, it at all.

My recommendation? Don't do it.

Craig Trader
A: 

IMO a better way to do this would be to include a hash generated off of the file size or a checksum based on the file contents or last-modified date. That way you don't have to update some version number and just let the number be driven off of the file's changing properties.

spig