javascript 如何强制 window.location 发出 HTTP 请求而不是使用缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14197276/
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-10-26 20:58:48  来源:igfitidea点击:

How can I force window.location to make an HTTP request instead of using the cache?

javascriptfirefoxbrowser

提问by Daniel Rikowski

In my web application I am setting window.locationto navigate to a different page, but for some reason Firefox shows an old version of that page.

在我的 Web 应用程序中,我设置window.location导航到不同的页面,但出于某种原因,Firefox 显示该页面的旧版本。

Using Firebug I detected that the browser doesn't even send an HTTP request, it simply uses an older version of that page (not even the last one) and displays it.

使用 Firebug 我检测到浏览器甚至不发送 HTTP 请求,它只是使用该页面的旧版本(甚至不是最后一个)并显示它。

The page itself has all the usual headersto prevent caching which works perfectly when I browse my pages using links or manual input. The problem only occurs when seting window.location.

页面本身具有所有常见的标题,以防止缓存在我使用链接或手动输入浏览页面时完美运行。该问题仅在设置window.location.

Is this a Firefox problem or something to expect from any browser? Can this behaviour be changed?

这是 Firefox 的问题还是任何浏览器的预期问题?这种行为可以改变吗?

回答by Sirko

You could just add a random parameter to the page URL in order to have the browser issue a new request.

您可以只向页面 URL 添加一个随机参数,以便让浏览器发出新请求。

So instead of using

所以而不是使用

 window.location = "my.url/index.html";

use

利用

 window.location = "my.url/index.html?nocache=" + (new Date()).getTime();

回答by Nick Bray

You can use location.reload with a true argument, which will always bypass the cache.

您可以使用带有 true 参数的 location.reload,这将始终绕过缓存。

window.location.reload(true);

回答by mortb

Add some time specific or random querystring value to the url. This will force the page to be reloaded.

向 url 添加一些特定时间或随机的查询字符串值。这将强制重新加载页面。

var yourNewLoc = "http://newlocation.com/";
document.location = yourNewLoc + "?c=" + (new Date()).valueOf();

回答by nairvijays

You have to validate if there is any existing query parameter in the url. If any query parameter exist, you should append the timestamp using "&". I have written a quick snippet that might be of your help.

您必须验证 url 中是否存在任何现有的查询参数。如果存在任何查询参数,则应使用“&”附加时间戳。我写了一个简短的片段,可能对你有帮助。

window.newLocation = function( location ) {
    var newLocation = ( typeof location === "string" ) ? location : window.location.href,
       appendType = ( newLocation.indexOf("?") < 0 ) ? "?" : "&";
    window.location = newLocation + appendType + "_t=" + (new Date()).getTime();
}

Usage:

用法:

newLocation("index.html") or window.newLocation("index.html") 
// opens "index.html?_t=<timstamp>"

newLocation("index.html?existingQuery=true") or window.newLocation("index.html?existingQuery=true") 
// opens "index.html?existingQuery=true&_t=<timstamp

newLocation() or window.newLocation() 
// opens existing window location with a timestamp

You could further modify the snippet to remove exiting timestamp in the query parameter to avoid duplication

您可以进一步修改代码片段以删除查询参数中的退出时间戳以避免重复

回答by zaffar

just need to tell your download function (in the controller, in case of Laravel) do not cache it by setting the headers, used the following code for Laravel:

只需要告诉你的下载函数(在控制器中,在 Laravel 的情况下)不要通过设置标题来缓存它,Laravel 使用以下代码:

$headers =[
            'Content-Type' => 'application/text',
            'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
            'Cache-Control' => 'post-check=0, pre-check=0, false',
             'Pragma' => 'no-cache',  ];
return response()->file($pathToFile, $headers);

This code is very much true for PhP as well just need transferring the code accordingly. By adding new dates may invalidate a link especially if you are using a temporarySignedLink etc.

这段代码非常适用于 PhP,也只需要相应地传输代码。添加新日期可能会使链接无效,尤其是在您使用临时签名链接等时。

Cheers

干杯