在 android 微信中用 location.reload()
刷新当前页面是不起作用的。
解决方法:
// 简单的匹配下连接符
var sign = /\?/.test(location.href) ? '&' : '?'
location.href = location.href + sign + 't=' + Date.now()
更新:
之前写的方法,遇到 hash 的情况就不好使了。而且没有对 t
参数做判断每次刷新都会不断往后加。简直就是 bug
,也不知道当时怎么用的。
function wxRefresh () {
let str = ''
if (location.search.indexOf('t=') > -1) {
str = location.search.replace(/t=\d+/, 't=' + Date.now())
} else {
let sign = /\?/.test(location.href) ? '&' : '?'
str = location.search + sign + 't=' + Date.now()
}
location.search = str
}
这个相对完善一些。测试在微信中可以正常刷新。
最近发现,不仅在微信中 reload 刷新会失效,在其他 app 中也有可能。
这是因为客户端做的优化,如果比较前后两个链接没有改变的话,默认会走本地缓存,不会发出请求。