cou*_*011 135 javascript internet-explorer
我想使用JavaScript重新加载页面,但我也要清除缓存,因此在页面刷新页面上有来自服务器的所有内容的最新版本.
除IE之外的其他浏览器没有获得最新内容.
IE9的任何解决方案?
Chr*_*ian 209
reload()
应该接受一个参数,告诉它做一个硬重载,即忽略缓存:
location.reload(true);
Run Code Online (Sandbox Code Playgroud)
我无法保证其可靠性,您可能希望进一步调查此问题.
Osc*_*son 55
你可以通过几种方式做到这一点.一,只需将此元标记添加到您的head
:
<meta http-equiv="Cache-control" content="no-cache">
Run Code Online (Sandbox Code Playgroud)
如果要从缓存中删除文档,expires
元标记应该通过将其content
属性设置为-1
如此来删除它:
<meta http-equiv="Expires" content="-1">
Run Code Online (Sandbox Code Playgroud)
http://www.metatags.org/meta_http_equiv_cache_control
此外,IE应该为您提供主页的最新内容.如果您遇到外部文档(如CSS和JS)的问题,请在URL的末尾添加一个虚拟参数,其当前时间以毫秒为单位,以便它永远不会相同.这样,IE和其他浏览器将始终为您提供最新版本.这是一个例子:
<script src="mysite.com/js/myscript.js?12345">
Run Code Online (Sandbox Code Playgroud)
更新1
阅读完评论后,我意识到你想以编程方式擦除缓存而不是每次都清除缓存.你可以做的是在JS中有一个函数:
eraseCache(){
window.location = window.location.href+'?eraseCache=true';
}
Run Code Online (Sandbox Code Playgroud)
然后,在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>
Run Code Online (Sandbox Code Playgroud)
这没有经过测试,但应该可行.基本上,如果调用了JS函数,它将重新加载页面,但会在URL的末尾添加一个GET参数.然后,您的站点将有一些查找此参数的后端代码.如果它存在,它会添加元标记和包含时间戳的缓存变量,并将其附加到您遇到缓存问题的脚本和CSS.
更新2
元标记确实不会擦除页面加载时的缓存.因此,从技术上讲,您需要在JS中运行eraseCache函数,一旦页面加载,您需要再次加载它以进行更改.您应该能够使用服务器端语言解决此问题.您可以运行相同的eraseCache 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... -->
Run Code Online (Sandbox Code Playgroud)
此方法可立即运行,无需页面重新加载,因为它会在页面加载之前以及在运行任何内容之前擦除缓存.
小智 8
我有这个问题,我用javascript解决了它
location.reload(true);
Run Code Online (Sandbox Code Playgroud)
你也可以用
window.history.forward(1);
Run Code Online (Sandbox Code Playgroud)
用户退出应用程序后停止浏览器后退按钮.
在我的情况下 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(/#.*$/, '');
};
Run Code Online (Sandbox Code Playgroud)
我编写了这个 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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
318592 次 |
最近记录: |