Internet Explorer (IE) and the Cached .js File CodeUnit 13 JAN 2011

Internet Explorer and pretty much all other modern web browsers make use of silent caching in order to streamline user experience. Of course, caching is a marvellous tool when needed, but also a frustrating one when you don’t want it turned on. Enter the humble .js javascript include file.

Now IE likes to cache javascript .js file includes, but unfortunately unlike normal web pages, getting it to refresh its cached copy can sometime prove to be quite difficult to achieve.

So here is the solution then.

Simply add a GET variable to the declaration.

If you never want the file to be cached, attach a randomly generated GET variable to the .js declaration for each page load. If you want to control when it gets refreshed, add a GET variable that you change only when you make actual changes to the .js file itself.

Essentially what happens is that IE recognises the URL as a single string, meaning that if the GET variable has changed, the string’s identity has changed, meaning a brand new URL and thus something it will need to go and collect from the internet.

So how does one’s .js file include look now?

<script type=”text/javascript” src=”myscript.js?ver=1″></script>

Simple, but very effective and well worth jotting down.


Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • Marc

    Hey Craig,

    I’ve stumbled on this post whilst trying to find a solution to fixing this!!

    I used this method of including a .js file but it seems to break the include in webkit browsers (chrome and safari) and the data is not pulled in.

    Have you come across this? Do you know how to fix it?

    I’m stumped!!



    • Strange. This is the call I use in my own work (for both .js and .css files) and which works fine in IE/Firefox/Chrome (tested):