Skip to main content

Tell us about your project

Close-up of white keyboard

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`ve set everything up, but then realized I was gonna get the same old comments from upstairs I always get when we change the JavaScript file:

- It doesn’t work for me!

- Shift+Reload!

- Thanks, 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>

into 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 off the E.”

So we never really used this and just let the Nature take its 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 gentlemen, the page will from now forth automatically force reload of a script file only when it has been changed.