微信的title只会在进入页面时读取一次,并不会触发title的change事件。
(因为刷新页面时能及时显示title,这个代码就是用iframe做了一次伪请求,在body中添加一个iframe标签,请求成功之后再移除dom)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function changeTitle(title) {
document.title = title
let body = document.body
let iframe = document.createElement('iframe')
iframe.src = '../../img/wh.png'
iframe.style.display = 'none'
body.appendChild(iframe)
if (iframe.attachEvent) {
body.attachEvent('onload', function() {
setTimeout(function() {
body.removeChild(iframe)
}, 0)
})
} else {
iframe.onload = function() {
setTimeout(function() {
body.removeChild(iframe)
}, 0)
}
}
}

已知bug:
在使用该方法后,有时会在页面下面出现白条,(虽然iframe已经加了display: none;并且已经删除),解决方法是在css中直接

1
2
3
body iframe {
display: none;
}