View Full Version : Prevent the caching of .js files

01-13-2010, 03:10 PM

Sorry if this has been covered here before - I did a quick forum search but didn't find the right answer.

Q. With an external .js file, where the filename remains constant but its contents vary over time, is it possible to ensure that the web browser always loads the current website version, rather than a cached copy?

I thought of using the pragma: no-cache HTML header but I'd rather not as I want images to be cached to ease page loading.

Thanks for your help...

01-13-2010, 04:50 PM
<script type="text/javascript" src="whatever.js"></script>
<script type="text/javascript">
s = s[s.length - 2];
s.src = s.src + '?bustcache=' + new Date().getTime();

Or, a server side language could be used if available, ex with PHP:

<script type="text/javascript" src="whatever.php"></script>

Just have a javascript and a no-cache header in the whatever.php file. The rest could be ordinary javascirpt.