登录

具有清除缓存的window.location.reload

内容来源于 Stack Overflow,遵循 CC BY-SA 4.0 许可协议进行翻译与使用。IT领域专用引擎提供翻译支持

腾讯云小微IT领域专用引擎提供翻译支持

原文
YakovL 修改于2017-09-01
  • 该问题已被编辑
  • 提问者: coure2011
  • 提问时间: 2011-04-20 03:38

我想使用JavaScript重新加载页面,但我也想清除缓存,因此在页面刷新时,页面将包含来自服务器的所有内容的最新版本。

除IE以外的其他浏览器无法获取最新内容。

对于IE9有什么解决方案吗?

浏览 572 关注 0 得票数 165
  • 得票数为Stack Overflow原文数据
原文
已采纳
修改于2021-08-18
  • 该回答已被编辑
  • 回答者: Christian
  • 回答时间: 2011-04-20 05:18
得票数 249

reload() 应该接受一个参数,该参数告诉它执行硬重新加载,即忽略缓存:

location.reload(true);

我不能保证它的可靠性,你可能需要进一步调查这一点。

编辑(2021):该参数从未标准化,已被弃用,并在更现代的浏览器中被删除。每季度添加一条评论来描述这一事实是没有帮助的。

修改于2013-12-09
  • 该回答已被编辑
  • 回答者: Birke
  • 回答时间: 2013-12-09 14:42
得票数 5

我编写了这个javascript脚本,并将其包含在头文件中(在加载之前)。它似乎起作用了。如果页面是一个多小时前加载的,或者情况不确定,它将从服务器重新加载所有内容。1小时= 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);
                return;
            document.cookie = "zeitstempel=" + new Date().getTime() + ";";
            location.reload(true);
        document.cookie = "zeitstempel=" + new Date().getTime() + ";";
        location.reload(true);
zeit();