Clear the cache in JavaScript

subramani picture subramani · Jun 18, 2009 · Viewed 490.9k times · Source

How do I clear a browsers cache with JavaScript?

We deployed the latest JavaScript code but we are unable to get the latest JavaScript code.

Editorial Note: This question is semi-duplicated in the following places, and the answer in the first of the following questions is probably the best. This accepted answer is no longer the ideal solution.

How to force browser to reload cached CSS/JS files?

How can I force clients to refresh JavaScript files?

Dynamically reload local Javascript source / json data

Answer

Kevin Hakanson picture Kevin Hakanson · Feb 2, 2010

You can call window.location.reload(true) to reload the current page. It will ignore any cached items and retrieve new copies of the page, css, images, JavaScript, etc from the server. This doesn't clear the whole cache, but has the effect of clearing the cache for the page you are on.

However, your best strategy is to version the path or filename as mentioned in various other answers. In addition, see Revving Filenames: don’t use querystring for reasons not to use ?v=n as your versioning scheme.