Javascript window.location.reload 清除缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5721704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.location.reload with clear cache
提问by coure2011
I want to reload a page using JavaScript but I want to clear cache too, so on page refresh the page has latest versions of everything from server.
Other browsers except IE are not getting latest content.
我想使用 JavaScript 重新加载页面,但我也想清除缓存,因此在页面刷新时,页面具有来自服务器的所有内容的最新版本。
除 IE 外的其他浏览器未获取最新内容。
Any solution for IE9?
IE9的任何解决方案?
回答by Christian
reload()
is supposed to accept an argument which tells it to do a hard reload, ie, ignoring the cache:
reload()
应该接受一个参数,告诉它进行硬重新加载,即忽略缓存:
location.reload(true);
I can't vouch for its reliability, you may want to investigate this further.
我不能保证它的可靠性,你可能想进一步调查。
回答by Oscar Godson
You can do this a few ways. One, simply add this meta tag to your head
:
您可以通过几种方式做到这一点。一,只需将此元标记添加到您的head
:
<meta http-equiv="Cache-control" content="no-cache">
If you want to remove the document from cache, expires
meta tag should work to delete it by setting its content
attribute to -1
like so:
如果您想从缓存中删除文档,expires
元标记应该通过将其content
属性设置为-1
像这样来删除它:
<meta http-equiv="Expires" content="-1">
http://www.metatags.org/meta_http_equiv_cache_control
http://www.metatags.org/meta_http_equiv_cache_control
Also, IE should give you the latest content for the main page. If you are having issues with external documents, like CSS and JS, add a dummy param at the end of your URLs with the current time in milliseconds so that it's never the same. This way IE, and other browsers, will alwaysserve you the latest version. Here is an example:
此外,IE 应该为您提供主页的最新内容。如果您在处理外部文档(如 CSS 和 JS)时遇到问题,请在您的 URL 末尾添加一个虚拟参数,以毫秒为单位显示当前时间,以使其永远不会相同。这样 IE 和其他浏览器将始终为您提供最新版本。下面是一个例子:
<script src="mysite.com/js/myscript.js?12345">
UPDATE 1
更新 1
After reading the comments I realize you wanted to programmatically erase the cache and not every time. What you could do is have a function in JS like:
阅读评论后,我意识到您想以编程方式擦除缓存,而不是每次都擦除。你可以做的是在 JS 中有一个函数,比如:
eraseCache(){
window.location = window.location.href+'?eraseCache=true';
}
Then, in PHP let's say, you do something like this:
然后,假设在 PHP 中,您执行以下操作:
<head>
<?php
if (isset($_GET['eraseCache'])) {
echo '<meta http-equiv="Cache-control" content="no-cache">';
echo '<meta http-equiv="Expires" content="-1">';
$cache = '?' . time();
}
?>
<!-- ... other head HTML -->
<script src="mysite.com/js/script.js<?= $cache ?>"
</head>
This isn't tested, but should work. Basically, your JS function, if invoked, will reload the page, but adds a GET param to the end of the URL. Your site would then have some back-end code that looks for this param. If it exists, it adds the meta tags and a cache variable that contains a timestamp and appends it to the scripts and CSS that you are having caching issues with.
这没有经过测试,但应该可以工作。基本上,您的 JS 函数(如果被调用)将重新加载页面,但会在 URL 的末尾添加一个 GET 参数。然后,您的站点将具有一些查找此参数的后端代码。如果存在,它会添加元标记和包含时间戳的缓存变量,并将其附加到您遇到缓存问题的脚本和 CSS。
UPDATE 2
更新 2
The meta tag indeed won't erase the cache on page load. So, technically you would need to run the eraseCache function in JS, once the page loads, you would need to load it againfor the changes to take place. You should be able to fix this with your server side language. You could run the same eraseCacheJS function, but instead of adding the meta tags, you need to add HTTP Cache headers:
元标记确实不会在页面加载时擦除缓存。因此,从技术上讲,您需要在 JS 中运行 eraseCache 函数,一旦页面加载,您将需要再次加载它以进行更改。您应该能够使用您的服务器端语言解决此问题。您可以运行相同的eraseCacheJS 函数,但不是添加元标记,而是需要添加 HTTP 缓存标头:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
?>
<!-- Here you'd start your page... -->
This method works immediately without the need for page reload because it erases the cache before the page loads and also before anything is run.
此方法无需重新加载页面即可立即生效,因为它会在页面加载之前以及运行任何内容之前擦除缓存。
回答by yousef
i had this problem and i solved it using javascript
我遇到了这个问题,我使用 javascript 解决了它
location.reload(true);
you may also use
你也可以使用
window.history.forward(1);
to stop the browser back button after user logs out of the application.
在用户退出应用程序后停止浏览器后退按钮。
回答by Rodrigo
In my case reload() doesn't work because the asp.net controls behavior. So, to solve this issue I've used this approach, despite seems a work around.
在我的情况下 reload() 不起作用,因为 asp.net 控制行为。所以,为了解决这个问题,我使用了这种方法,尽管似乎是一种解决方法。
self.clear = function () {
//location.reload(true); Doesn't work to IE neither Firefox;
//also, hash tags must be removed or no postback will occur.
window.location.href = window.location.href.replace(/#.*$/, '');
};
回答by Birke
I wrote this javascript script and included it in the header (before anything loads). It seems to work. If the page was loaded more than one hour ago or the situation is undefined it will reload everything from server. The time of one hour = 3600000 milliseconds can be changed in the following line: if(alter > 3600000)
我编写了这个 javascript 脚本并将其包含在标题中(在加载任何内容之前)。它似乎工作。如果页面在一个多小时前加载或情况未定义,它将从服务器重新加载所有内容。一小时 = 3600000 毫秒的时间可以在以下行中更改: if(alter > 3600000)
With regards, Birke
对此,伯克
<script type="text/javascript">
//<![CDATA[
function zeit()
{
if(document.cookie)
{
a = document.cookie;
cookiewert = "";
while(a.length > 0)
{
cookiename = a.substring(0,a.indexOf('='));
if(cookiename == "zeitstempel")
{
cookiewert = a.substring(a.indexOf('=')+1,a.indexOf(';'));
break;
}
a = a.substring(a.indexOf(cookiewert)+cookiewert.length+1,a.length);
}
if(cookiewert.length > 0)
{
alter = new Date().getTime() - cookiewert;
if(alter > 3600000)
{
document.cookie = "zeitstempel=" + new Date().getTime() + ";";
location.reload(true);
}
else
{
return;
}
}
else
{
document.cookie = "zeitstempel=" + new Date().getTime() + ";";
location.reload(true);
}
}
else
{
document.cookie = "zeitstempel=" + new Date().getTime() + ";";
location.reload(true);
}
}
zeit();
//]]>
</script>