Bdfy Bdfy - 3 years ago 146
Javascript Question

How to prevent caching of my Javascript file?

I have a simple html:

<meta charset="utf-8">
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<script src="test.js"></script>

In test.js I changed a Javascript function, but my browser is caching this file. How to disable cache for script src?

Update: How to add a random script using javascript ?

Answer Source

Add a random query string to the src

You could either do this manually by incrementing the querystring each time you make a change:

<script src="test.js?version=1"></script>

Or if you are using a server side language, you could automatically generate this:


<script src="test.js?rndstr=<%= getRandomStr() %>"></script>

More info on cache-busting can be found here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download