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?

One comment

  1. Alison on August 20th, 2008 at 12:13 am

Leave a comment

Comments RSS Feed

Recent Comments

  • luciano: Is there any way to fit the submenu elements to the widthest element in each one? I don´t know if I make...
  • g25: I agree that Apple should implement a homescreen like this. Personally though the lock screen should be like...
  • wildspirit: C++ : http://wallpaper.wallpedia.org /24__boat_destroyer.htm
  • tom: Hello, this menue is great, but i noticed that it does not work in Opera perfectly. The “hover”...
  • medbrooro: adam ant shoes http://topcitystyle.com/dark-b rown-women-color29.html graphic designer gucci purses...