javascript - history.replaceState() 例子?

标签 javascript ajax html

任何人都可以为 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/

相关文章:

javascript - Rails3 中的 Google Instant 式搜索

ajax - 在 mvc 中,将表单(什么 Controller )的 ajax 脚本放在哪里?

javascript - 如何比较当前变量和当前类型?

javascript - 递归范围函数不起作用

javascript - g.raphael.js 饼图渐变效果

php - JQuery .post - $_POST 为空

jquery - 当用户从我的网页复制一些文本时,用于在剪贴板中添加文本的脚本

javascript - 语法错误 unrecognized expression wrap html tag to string

javascript - 解析 Promise,destroyAll();不工作?

javascript - firebase 函数中无法访问全局变量