我想使用JavaScript重新加载页面,但我也想清除缓存,因此在页面刷新时,页面具有服务器中所有内容的最新版本。
除IE之外的其他浏览器均未获取最新内容。
IE9有什么解决方案吗?
reload()
应该接受一个告诉它进行硬重装的参数,即忽略缓存:
location.reload(true);
我不能保证它的可靠性,您可能需要进一步调查。
您可以通过几种方法来执行此操作。一,只需将此meta标签添加到您的中head
:
<meta http-equiv="Cache-control" content="no-cache">
如果要从缓存中删除文档,则expires
meta标签应通过将其content
属性设置为来删除它,-1
如下所示:
<meta http-equiv="Expires" content="-1">
http://www.metatags.org/meta_http_equiv_cache_control
另外,IE应该为您提供主页的最新内容。如果您对外部文档(如CSS和JS)有疑问,请在URL的末尾添加一个虚拟参数,以毫秒为单位,以确保当前时间不变。这样,IE和其他浏览器将始终为您提供最新版本。这是一个例子:
<script src="mysite.com/js/myscript.js?12345">
更新1
阅读注释后,我意识到您想以编程方式而不是每次都擦除缓存。您可以做的是在JS中有一个函数,例如:
eraseCache(){
window.location = window.location.href+'?eraseCache=true';
}
然后,在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>
这未经测试,但应该可以。基本上,您的JS函数(如果被调用)将重新加载页面,但会在URL末尾添加GET参数。然后,您的站点将具有一些用于查找此参数的后端代码。如果存在,它将添加元标记和包含时间戳的缓存变量,并将其附加到您遇到缓存问题的脚本和CSS。
更新2
meta标签确实不会在页面加载时清除缓存。因此,从技术上讲,您将需要在JS中运行deleteCache函数,一旦页面加载,您将需要再次加载它以进行更改。您应该能够使用服务器端语言解决此问题。您可以运行相同的aseaseCache JS函数,但无需添加元标记,而需要添加HTTP Cache标头:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
?>
<!-- Here you'd start your page... -->
此方法无需页面重新加载即可立即工作,因为它会在页面加载之前以及运行任何内容之前清除缓存。
我有这个问题,我用JavaScript解决了
location.reload(true);
您也可以使用
window.history.forward(1);
在用户退出应用程序后停止浏览器后退按钮。
在我的情况下,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(/#.*$/, '');
};
我写了这个JavaScript脚本,并将其包含在标题中(未加载任何内容)。似乎有效。如果页面已加载一个小时以上,或者情况未定义,它将从服务器重新加载所有内容。可以在以下行中更改一小时的时间= 3600000毫秒:if(alter> 3600000)
问候,伯克
<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>
文章标签:internet-explorer , javascript
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!