在Chrome中使用Javascript强制重新加载页面[无缓存] [英] Force a reload of page in Chrome using Javascript [no cache]

查看:381
本文介绍了在Chrome中使用Javascript强制重新加载页面[无缓存]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JavaScript重新加载页面,并确保它不会从浏览器缓存中拉出,而是从服务器重新加载页面。
[由于页面元素在临时过程中会发生变化]

在IE和FF上,我发现以下代码工作正常;

  window.location.reload(true); 

然而,它不适用于Chrome或Safari。



我尝试了以下方法,但也无济于事;

  window.location.replace(location.href); 
document.location.reload(true);
document.location.replace(location.href);

是否有解决此问题的方法?



结果



经过调查,我发现这个问题是HTTP协议处理;


  1. Chrome通过 Pragma:no-cache HTTP字段发送请求

  2. 服务器响应使用 Last-Modified:DATE1 字段

  3. JS使用 location.reload(true)强制从服务器重新加载缓存而非缓存

  4. Chrome通过 If-Modified-Since:DATE1 字段发送请求

  5. 服务器响应 HTTP状态304未修改

服务器应用程序存在错误,因为没有注意到动态页面内容中的状态更改,因此不会返回 200
但是,当JS location.reload(true)时,Chrome / WebKit是唯一一个发送 If-Modified-Since 字段的浏览器。



我想我会把我的发现放在这里,以防别人遇到同样的问题。

解决方案

您可以使用这个破解:

  $ .ajax({
url:window.location.href,
headers:{
Pragma:no-cache,
Expires:-1,
Cache-Control:no-cache
}
})。done(function(){
window.location.reload(true);
}) ;


I need to reload a page using JavaScript and ensure that it does not pull from the browser cache but instead reloads the page from the server. [As elements of the page will have changed in the interim]

On IE and FF I found that the following code worked fine;

window.location.reload(true);

However it does not work on Chrome or Safari.

I tried the following, but also to no avail;

window.location.replace(location.href);
document.location.reload(true);
document.location.replace(location.href);

Is there a solution to this issue?

Findings

After looking into this I have found that this issue is HTTP Protocol handling;

  1. Chrome sends a request with Pragma: no-cache HTTP field
  2. Server responds with Last-Modified: DATE1 field
  3. JS uses location.reload(true) to force a reload from server not cache
  4. Chrome sends a request with If-Modified-Since: DATE1 field
  5. Server responds with HTTP Status 304 Not Modified

The server application is at fault for not noticing the state change in the dynamic page content, and thus not returning a 200. However, Chrome/WebKit is the only browser that sends a If-Modified-Since field when the JS location.reload(true) is called.

I thought I would put my findings here in-case someone else comes across the same issue.

解决方案

You can use this hack:

 $.ajax({
        url: window.location.href,
        headers: {
            "Pragma": "no-cache",
            "Expires": -1,
            "Cache-Control": "no-cache"
        }
    }).done(function () {
        window.location.reload(true);
    });

这篇关于在Chrome中使用Javascript强制重新加载页面[无缓存]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆