The death of the Shift+Reload

So… another EXIT season is beginning and we’ve started to clean up the festival’s main website. Changed some photos, added a faded overlay background and a cool autostarting promo video. I set everything up, but then realized I was gonna get the same old emails from upstairs I always get when we change the javascript file: “It doesn’t work for me!” -Shift+Reload! “Works!”. The browser caches the script file for an undeterminable amount of time, and the new one isn’t loaded until it expires or is forced down the drain. So what about our users? They might experience the same thing. The usual solution is simple - just append a variable in a form of a random set of characters to the URL. Change this

  1. <script type="text/javascript" src="script.js"></script>

to this

  1. <script type="text/javascript" src="script.js?12345"></script>

or this (for automation)

  1. <script type="text/javascript" src="script.js?<?php print time() ?>"></script>

The problem with this method is that now the script file will never be cached, or rather, reloaded every time a user visits the page, unless he loses interest, goes away and then comes back to check out what’s new, and than realizes, like “Wow…it’s still the same second?! Cause I noticed the script file hasn’t reloaded. Man, I left like ages ago. I should really lay of the E.” So we never really used this and just let nature take it’s course. But then it came to me…

  1.  <script type="text/javascript" src="script.js?<?php print filectime('script.js') ?>"></script>

This function returns the change time of a file, the script.js file in this case. Ladies and gentleman, the page will from now forth automatically force reload of a script file only when it has been changed.


back to top