简体   繁体   English

javascript-关闭浏览器时清除缓存的代码

[英]javascript - code to clear the cache on closing the browser

In my web application I wants the java script to clear the cache when the user close the browser. 在我的Web应用程序中,我希望Java脚本在用户关闭浏览器时清除缓存。

The javascript should listen to the event and clear the cache. javascript应该侦听事件并清除缓存。

Can anyone please provide me sample code or any useful link? 有人可以提供示例代码或任何有用的链接吗?

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 这是供参考的文章http://www.htmlgoodies.com/beyond/reference/article.php/3472881

For clearing Cache followiwng link is helpful Clear the cache in JavaScript 对于清除Cache followiwng链接很有用清除JavaScript中的缓存

You write some javascript run when you try to close the browser like below 当您尝试关闭浏览器时,您编写了一些JavaScript运行,如下所示

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>

With JavaScript its not possible. 使用JavaScript不可能。 But using no-cache header you can do it. 但是使用no-cache标头就可以做到。

Use one of these methods which work on all browsers 使用在所有浏览器上均可使用的以下方法之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM