gpt4 book ai didi

javascript - history.replaceState() 例子?

转载 作者:IT王子 更新时间:2023-10-29 02:39:30 29 4
gpt4 key购买 nike

任何人都可以为 history.replaceState 提供一个工作示例吗?这就是w3.org说:

history.replaceState(data, title [, url ] )

Updates the current entry in the session history to have the given data, title, and, if provided and not null, URL.


更新

这非常有效:

history.replaceState( {} , 'foo', '/foo' );

URL 在变,但标题没有变。那是错误还是我遗漏了什么?在最新的 Chrome 上测试。

最佳答案

确实这是一个错误,尽管现在已经有意为之 2 年了。问题在于一些不明确的规范以及涉及 document.title 和后退/前进时的复杂性。

请参阅 Webkit 上的错误引用和 Mozilla .还有Opera关于History API的介绍said it wasn't using the title parameter并且可能仍然没有。

Currently the 2nd argument of pushState and replaceState — the title of the history entry — isn't used in Opera's implementation, but may be one day.

可能的解决方案

我看到的唯一方法是更改​​ title 元素并改用 pushState:

document.getElementsByTagName('title')[0].innerHTML = 'bar';
window.history.pushState( {} , 'bar', '/bar' );

关于javascript - history.replaceState() 例子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12832317/

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