如何通过JavaScript强制web浏览器对页面进行硬刷新? 硬刷新意味着获取页面的新副本,并刷新所有外部资源(图像、JavaScript、CSS等)。
当前回答
对于angular用户,你可以执行以下操作:
<form [action]="myAppURL" method="POST" #refreshForm></form>
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
// ...
})
export class FooComponent {
@ViewChild('refreshForm', { static: false }) refreshForm;
forceReload() {
this.refreshForm.nativeElement.submit();
}
}
它起作用的原因在这个网站上有解释:https://www.xspdf.com/resolution/52192666.html
在本文中,您还将了解到硬重载是如何为每个框架工作的
解释:角
Location: reload(), Location.reload()方法重载当前URL,就像Refresh按钮一样。只使用location.reload();如果你想执行强制重载(例如使用Ctrl + F5)来重载服务器而不是浏览器缓存中的所有资源,这不是一个解决方案。这个问题的解决方案是,执行一个POST请求到当前位置,因为这总是使浏览器重新加载所有内容。
其他回答
我发现的最可靠的方法是通过向查询字符串添加值来使用chache buster。
下面是我常用的一个常规程序:
function reloadUrl() {
// cache busting: Reliable but modifies URL
var queryParams = new URLSearchParams(window.location.search);
queryParams.set("lr", new Date().getTime());
var query = queryParams.toString();
window.location.search = query; // navigates
}
调用这个函数会产生如下结果:
https://somesite.com/page?lr=1665958485293
重载后。
这可以强制每次重新加载,但需要注意的是URL会更改。在大多数应用程序中,这并不重要,但如果服务器依赖于特定的参数,这可能会导致潜在的副作用。
⚠️此解决方案并不适用于所有浏览器。location.reload()的MDN页面: 注意:Firefox支持location.reload()的非标准forceGet布尔参数,它告诉Firefox绕过缓存并强制重载当前文档。但是,在所有其他浏览器中,您在location.reload()调用中指定的任何参数都将被忽略,并且没有任何类型的影响。
Try:
location.reload(true);
当此方法接收到一个真值作为参数时,它将导致页面总是从服务器重新加载。如果为false或未指定,浏览器可能会从缓存中重新加载页面。
更多信息:
location对象
window.location.href = window.location.href
对于angular用户,你可以执行以下操作:
<form [action]="myAppURL" method="POST" #refreshForm></form>
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
// ...
})
export class FooComponent {
@ViewChild('refreshForm', { static: false }) refreshForm;
forceReload() {
this.refreshForm.nativeElement.submit();
}
}
它起作用的原因在这个网站上有解释:https://www.xspdf.com/resolution/52192666.html
在本文中,您还将了解到硬重载是如何为每个框架工作的
解释:角
Location: reload(), Location.reload()方法重载当前URL,就像Refresh按钮一样。只使用location.reload();如果你想执行强制重载(例如使用Ctrl + F5)来重载服务器而不是浏览器缓存中的所有资源,这不是一个解决方案。这个问题的解决方案是,执行一个POST请求到当前位置,因为这总是使浏览器重新加载所有内容。
这是2022年的更新,有2种方法,考虑到url中有#的SPA:
方法1:
正如在其他答案中提到的,一种解决方案是将一个随机参数放入查询字符串。在javascript中,它可以这样实现:
function urlWithRndQueryParam(url, paramName) {
const ulrArr = url.split('#');
const urlQry = ulrArr[0].split('?');
const usp = new URLSearchParams(urlQry[1] || '');
usp.set(paramName || '_z', `${Date.now()}`);
urlQry[1] = usp.toString();
ulrArr[0] = urlQry.join('?');
return ulrArr.join('#');
}
function handleHardReload(url) {
window.location.href = urlWithRndQueryParam(url);
// This is to ensure reload with url's having '#'
window.location.reload();
}
handleHardReload(window.location.href);
坏的部分是,它改变了当前的url,有时,在干净的url,它可能看起来有点难看的用户。
方法2:
从https://splunktool.com/force-a-reload-of-page-in-chrome-using-javascript-no-cache的想法来看,这个过程可以是先获得没有缓存的url,然后重新加载页面:
async function handleHardReload(url) {
await fetch(url, {
headers: {
Pragma: 'no-cache',
Expires: '-1',
'Cache-Control': 'no-cache',
},
});
window.location.href = url;
// This is to ensure reload with url's having '#'
window.location.reload();
}
handleHardReload(window.location.href);
甚至可以结合方法1,但我认为与头应该足够:
async function handleHardReload(url) {
const newUrl = urlWithRndQueryParam(url);
await fetch(newUrl, {
headers: {
Pragma: 'no-cache',
Expires: '-1',
'Cache-Control': 'no-cache',
},
});
window.location.href = url;
// This is to ensure reload with url's having '#'
window.location.reload();
}
handleHardReload(window.location.href);
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie