History:replaceState() 方法
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
History 接口的 replaceState() 方法使用状态对象和 URL 作为参数来修改当前的历史记录条目。当你想要更新当前历史记录条目的状态对象或者 URL 来响应用户的动作时,这个方法非常有用。
语法
replaceState(state, unused)
replaceState(state, unused, url)
参数
返回值
无(undefined)。
异常
SecurityErrorDOMException-
如果关联的文档没有完全激活,或者提供的
url参数不是有效的 URL,则抛出该异常。 DataCloneErrorDOMException-
如果提供的
state参数不可序列化,则抛出该异常。
示例
假设 https://www.mozilla.org/foo.html 执行了以下 JavaScript 代码:
const stateObj = { foo: "bar" };
history.pushState(stateObj, "", "bar.html");
在下一个页面,你可以使用 history.state 来访问刚刚添加的 stateObj。
上面这两行的解释可以在使用历史记录 API 一文中找到。然后假设 https://www.mozilla.org/bar.html 执行了下面的 JavaScript 代码:
history.replaceState(stateObj, "", "bar2.html");
这会让 URL 栏显示 https://www.mozilla.org/bar2.html,但是不会加载 bar2.html 页面,甚至不会检查 bar2.html 是否存在。
假设用户跳转到 https://www.microsoft.com,然后点击返回按钮。这时,URL 栏将会显示 https://www.mozilla.org/bar2.html。如果用户此时再次点击返回按钮,URL 栏将会显示 https://www.mozilla.org/foo.html 页面,而完全绕过了 bar.html 页面。
规范
| Specification |
|---|
| HTML> # dom-history-replacestate-dev> |
浏览器兼容性
Loading…