History.replaceState()
method modifies the current history entry, replacing it with the
stateObj
,
title
,和
URL
passed in the method parameters. This method is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.
history.replaceState(stateObj, title, [url])
stateObj
replaceState
method. The state object can be
null
.
title
url
可选
The URL of the history entry. The new URL must be of the same origin as the current URL; otherwise replaceState throws an exception.
Suppose https://www.mozilla.org/foo.html executes the following JavaScript:
const stateObj = { foo: 'bar' };
history.pushState(stateObj, '', 'bar.html');
The explanation of these two lines above can be found in the
范例
pushState()
方法
章节的
处理历史的 API
article. Then suppose
https://www.mozilla.org/bar.html
executes the following JavaScript:
history.replaceState(stateObj, '', 'bar2.html');
This will cause the URL bar to display
https://www.mozilla.org/bar2.html
, but won't cause the browser to load
bar2.html
or even check that
bar2.html
exists.
Suppose now that the user navigates to https://www.microsoft.com , then clicks the Back button. At this point, the URL bar will display https://www.mozilla.org/bar2.html. If the user now clicks Back again, the URL bar will display https://www.mozilla.org/foo.html, and totally bypass bar.html.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'History.replaceState()' in that specification. |
实时标准 | 无变化自 HTML5 . |
|
HTML5
The definition of 'History.replaceState()' in that specification. |
推荐 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
replaceState
|
Chrome 5 | Edge 12 |
Firefox
4
|
IE 10 | Opera 11.5 | Safari 5 | WebView Android ≤37 | Chrome Android 18 |
Firefox Android
4
|
Opera Android 11.5 | Safari iOS 4.3 | Samsung Internet Android 1.0 |
是否
title
argument is used
|
Chrome No | Edge No | Firefox No | IE No | Opera No | Safari Yes | WebView Android No | Chrome Android No | Firefox Android No | Opera Android No | Safari iOS ? | Samsung Internet Android No |
完整支持
不支持
兼容性未知
见实现注意事项。