Posted By

jadedbat on 03/09/09


Tagged

css javascript query cache force


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

coprolit


Force server to load latest CSS file


 / Published in: CSS
 

URL: http://www.thatgrafix.com

This is sometimes not the best solution to a server caching your css files. But if you want to force a stubborn server to serve the most recent css file to the browser this seems to work. Could slow things down a touch, but for small css files not really noticeable.

Just change the var css to match the path to your css file, and then offer a non jscript version pointing to the same file.

Essentially by adding the query to the end of the css file the server thinks it needs to load it. I used date and getTime to create a number combo and tagged it to the end.

Use: put the following code into your right where your normal tag would go to point to your css file.

  1. <script type="text/javascript">
  2. // create a number with Date and getTime
  3. var x = new Date().getTime();
  4.  
  5. // create a variable with the path to your css file?=
  6. var css="css/styles.css?="+x;
  7.  
  8. // write the link tag with new css var
  9. document.writeln('<link rel="stylesheet" type="text/css" href="'+css+'">');
  10. </script>
  11. <noscript><link rel="stylesheet" type="text/css" media="all" href="css/styles.css" /></noscript>

Report this snippet  

You need to login to post a comment.