1

In my web application I wants the java script to clear the cache when the user close the browser.

The javascript should listen to the event and clear the cache.

Can anyone please provide me sample code or any useful link?

user1514499
  • 732
  • 5
  • 26
  • 58

4 Answers4

7

Technically is impossible. What you can do instead is to tell the browser to not cache the page by using the following meta tags:

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

Here is an article for reference http://www.htmlgoodies.com/beyond/reference/article.php/3472881

Endre Simo
  • 10,388
  • 2
  • 34
  • 44
1

For clearing Cache followiwng link is helpful Clear the cache in JavaScript

You write some javascript run when you try to close the browser like below

window.onbeforeunload = function ()
{
    // write your code here
 }

But why do you need to clear cache before closing browser?

Community
  • 1
  • 1
Narendra
  • 2,983
  • 7
  • 24
  • 49
1

Yes this is imposible but I would sugguest you can try the meta tags or you can set a versioning system for instance like this

<?php
session_start();
$versionNumber = '';
if(!isset($_SESSION['versionNumber']))
{
    $versionNumber = time() ;
    $_SESSION['versionNumber'] = $versionNumber;
}
else
{
    $versionNumber = $_SESSION['versionNumber'];
}
?>
<script src="home.js?v=<?php echo $versionNumber; ?>" ></script>
Waqar Alamgir
  • 8,752
  • 4
  • 25
  • 35
1

With JavaScript its not possible. But using no-cache header you can do it.

Use one of these methods which work on all browsers

Community
  • 1
  • 1
kiranvj
  • 22,650
  • 5
  • 51
  • 69