javascript - code to clear the cache on closing the browser

左心房为你撑大大i 提交于 2019-12-07 07:21:20

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

Narendra

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?

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>
kiranvj

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

Use one of these methods which work on all browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!