tags:

views:

3548

answers:

8

how to clear the cache in javascript?

We deployed the latest JavaScript code but we unable to get the latest javascript code

Can anyone help us?

+1  A: 

Ctrl + F5 isn't working?

It depends on what browser you're using, but in Firefox you can clear the cache with Tools -> Clear Private Data...

David Johnstone
+18  A: 

You can't clear the cache with javascript. A common way is to append the revision number or last updated timestamp to the file, like this:

myscript.123.js

or

myscript.js?updated=1234567890

Greg
+10  A: 

Try changing the JavaScript file's src? From this:

<script language="JavaScript" src="js/myscript.js"></script>

To this:

<script language="JavaScript" src="js/myscript.js?n=1"></script>

This method should force your browser to load a new copy of the JS file.

Barry Gallagher
Thanks now its working fine.
A: 

I tend to version my framework then apply the version number to script and style paths

<cfset fw.version = '001' />
<script src="/scripts/#fw.version#/foo.js"/>
SpliFF
cfset? What is that?
AnthonyWJones
cfset - http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt35.htm
Vijay Dev
A: 

You can also force the code to be reloaded every hour, like this, in PHP :

<?php
echo '<script language="JavaScript" src="js/myscript.js?token='.date('YmdH').'">';
?>
Fabien Ménager
+1  A: 

Here's a snippet of what I'm using for my latest project.

From the controller:

if ( IS_DEV ) {
    $this->view->cacheBust = microtime(true);
} else {
    $this->view->cacheBust = file_exists($versionFile) 
     // The version file exists, encode it
     ? urlencode( file_get_contents($versionFile) )
     // Use today's year and week number to still have caching and busting 
     : date("YW");
}

From the view:

<script type="text/javascript" src="/javascript/somefile.js?v=<?= $this->cacheBust; ?>"></script>
<link rel="stylesheet" type="text/css" href="/css/layout.css?v=<?= $this->cacheBust; ?>">

Our publishing process generates a file with the revision number of the current build. This works by URL encoding that file and using that as a cache buster. As a fail-over, if that file doesn't exist, the year and week number are used so that caching still works, and it will be refreshed at least once a week.

Also, this provides cache busting for every page load while in the development environment so that developers don't have to worry with clearing the cache for any resources (javascript, css, ajax calls, etc).

Justin Johnson
+1  A: 

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 affect 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.

Kevin Hakanson
A: 

@Barry Gallagher Thank you! it works! many thanks. Btw do this technique works on css too?

Halim
this is a comment, not an anwser
Mef
(a) This should be a comment; (b) of course it works on CSS too, and on images, and on any other resource which may be cached.
TRiG