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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10719505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:39:05  来源:igfitidea点击:

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

javascriptgoogle-chromesafariwebkitbrowser-cache

提问by graney

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]

我需要使用 JavaScript 重新加载页面,并确保它不会从浏览器缓存中提取,而是从服务器重新加载页面。[因为页面的元素将在此期间发生变化]

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-cacheHTTP field
  2. Server responds with Last-Modified: DATE1field
  3. JS uses location.reload(true)to force a reload from server not cache
  4. Chrome sends a request with If-Modified-Since: DATE1field
  5. Server responds with HTTP Status 304 Not Modified
  1. Chrome 发送带有Pragma: no-cacheHTTP 字段的请求
  2. 服务器用Last-Modified: DATE1字段响应
  3. JS 用于location.reload(true)强制从服务器重新加载而不是缓存
  4. Chrome 发送带有If-Modified-Since: DATE1字段的请求
  5. 服务器响应 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-Sincefield when the JS location.reload(true)is called.

服务器应用程序有问题,因为没有注意到动态页面内容中的状态变化,因此没有返回200. 但是,Chrome/WebKit 是唯一If-Modified-Sincelocation.reload(true)调用JS 时发送字段的浏览器。

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

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

回答by Suhan

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

回答by m03geek

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

回答by Roy Ling

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 请求,而是重复最后一个请求。

回答by Juergen Riemer

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

回答by TW147

Try window.location = window.location

尝试 window.location = window.location