简体   繁体   English

使用 Javascript 强制在 Chrome 中重新加载页面 [无缓存]

[英]Force a reload of page in Chrome using Javascript [no cache]

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.我需要使用 JavaScript 重新加载页面,并确保它不会从浏览器缓存中提取,而是从服务器重新加载页面。 [As elements of the page will have changed in the interim] [因为页面的元素将在此期间发生变化]

On IE and FF I found that the following code worked fine;在 IE 和 FF 上,我发现以下代码运行良好;

window.location.reload(true);

However it does not work on Chrome or Safari.但是它不适用于 Chrome 或 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;调查之后,我发现这个问题是 HTTP 协议处理;

  1. Chrome sends a request with Pragma: no-cache HTTP field Chrome 使用Pragma: no-cache HTTP 字段发送请求
  2. Server responds with Last-Modified: DATE1 field服务器响应Last-Modified: DATE1字段
  3. JS uses location.reload(true) to force a reload from server not cache JS 使用location.reload(true)强制从服务器重新加载而不是缓存
  4. Chrome sends a request with If-Modified-Since: DATE1 field Chrome 发送带有If-Modified-Since: DATE1字段的请求
  5. Server responds with HTTP Status 304 Not Modified服务器响应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 .服务器应用程序有问题,因为没有注意到动态页面内容中的状态变化,因此没有返回200 However, Chrome/WebKit is the only browser that sends a If-Modified-Since field when the JS location.reload(true) is called.但是,Chrome/WebKit 是唯一在调用 JS location.reload(true)时发送If-Modified-Since字段的浏览器。

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);
    });

To ensure the page isn't loaded from cache you can add some unique number to query:为了确保页面不是从缓存加载的,你可以添加一些唯一的数字来查询:

window.location = location.href + '?upd=' + 123456;

You also can use date instead of 123456您也可以使用日期代替 123456

Great findings!伟大的发现! I just encountered the same issue and this really helps a lot!我刚刚遇到了同样的问题,这真的很有帮助! However, in addition to your finding, it seems that Chrome always sends a GET request for location.reload()...IE/FF is repeating the last request instead.但是,除了您的发现之外,Chrome 似乎总是为 location.reload()...IE/FF 发送一个 GET 请求,而是重复最后一个请求。

This is what I do to ensure my application file is force reloaded on chrome:这是我为确保我的应用程序文件被强制重新加载到 chrome 所做的工作:

    var oAjax = new XMLHttpRequest;
    oAjax.open( 'get', '/path/to/my/app.js' );
    oAjax.setRequestHeader( 'Pragma', 'no-cache' );
    oAjax.send();
    oAjax.onreadystatechange = function() {
        if( oAjax.readyState === 4 ) {
            self.location.reload();
        }
    }

试试window.location = window.location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM