Timestamping Your CSS

Posted on May 1st, 2008, by Cristian in Xhtml & CSS

When you are developing a site, there is a heck of a lot of “refreshing” going on. You start to get a pretty good feel for what your browser is going to pick up on a single refresh, and what it won’t. For example, I find that if I over-write an image file on the server, it will take me two refreshes for that image to update on the live site.

There is a little trick to prevent CSS caching that I can be used also on javascript scripts. To prevent the caching I add a timestamp ( I used a Unix Timestamp) to the end of the src link.

<link rel=“stylesheet” type=“text/css” href=“style.css?<?=time()?> />

Which results in this:

<link rel=“stylesheet” type=“text/css” href=”style.css?1138618081 />

The theory here is that that link will change every second, and the browser will be tricked into thinking this is a new stylesheet and loading it fresh every time. Jason Edmond Beaird had the same idea and even created a little bookmarklet to force it.

My tests suggest that it works pretty well. What do you think? Do you know a better/smarter/faster way to do it?

Leave a comment

Advertise / Sponsors

How to Be a Rockstar Freelancer
Comments RSS Feed

Recent Comments

  • Julian Gruber: Thank you! Following those advices really can make a big difference for the success of a webapp.
  • Tom Humes: Nice Site layout for your blog. I am looking forward to reading more from you. Tom Humes
  • admin: Yes, it does the same. Basic I just get the object $(”#nicemenu span.head_menu”) once, and applied the...
  • André Beaudry: Hi, I would like to know why : $(”#nicemenu span.head_menu”).mouseov er(function(){...
  • admin: Try to change the z-index to the menu container in my example div #nicemenu. The first one should have for ex....