Thursday, November 11, 2010

9:25 PM
1
Some websites use highly volatile, oft-changing CSS and JavaScript files. In the case of these files, it's important that the developer prevent browsers from caching them. How do we do that? By using a phantom querystring, of course. We'll use PHP to tack the current time onto the file reference.

The PHP


<link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" /&glt;

<-- RENDERS -->

<link href="/stylesheet.css?1234567890" rel="stylesheet" type="text/css" /&glt;


<script type="text/javascript" src="/site-script.js?<?php echo time(); ?>"></script> 

<-- RENDERS -->

<script type="text/javascript" src="/site-script.js?1234567890"></script>


It's a very simple technique and doesn't affect your CSS or JavaScript code in any way.

1 comments:

Xwero said...

A better way is to use the filemtime function. Then users only need to download the file when it has actually changed.