gpt4 book ai didi

javascript - 不支持 history.pushState 的标题,什么是好的选择?

转载 作者:技术小花猫 更新时间:2023-10-29 12:27:18 26 4
gpt4 key购买 nike

History.pushState的第二个参数History.replaceState 可用于设置历史条目的“标题”。

这意味着当用户点击第 1 页到第 8 页时,他应该在历史栏中看到:

enter image description here

它正在 Safari 和 Opera 上运行。

但是在 Chrome 和 FireFox 上,这是用户看到的:

enter image description here

尝试更改 document.title 不起作用,因为它会更改历史标题中的所有条目:

enter image description here

这个问题的最佳解决方案是什么?

对于使用 pushStatereplaceState 实现的页面,我们是否被迫只使用一个历史标题?

最佳答案

我遇到了同样的问题,看来你错了。

如果 History.js 确实支持它,您也可以。通过查看源代码,history JS 似乎是这样做的:

https://github.com/browserstate/history.js/blob/master/scripts/uncompressed/history.js#L1293

try {
document.getElementsByTagName('title')[0].innerHTML = title.replace('<','&lt;').replace('>','&gt;').replace(' & ',' &amp; ');
}
catch ( Exception ) { }
document.title = title;

我测试过,Chrome 对我来说效果很好:它不会“重写”整个历史标题。然而,似乎后退或前进会导致页面重新加载,最终会重新初始化该标题。

关于javascript - 不支持 history.pushState 的标题,什么是好的选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26324990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com