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
The state object is a JavaScript object which is associated with the history entry passed to the replaceState method. The state object can be null .
title
Most browsers currently ignore this parameter , although they may use it in the future. Passing the empty string here should be safe against future changes to the method. Alternatively, you could pass a short title for the state.
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.
推荐 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
replaceState Chrome 5 Edge 12 Firefox 4
4
Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm . This allows a wider variety of objects to be safely passed.
IE 10 Opera 11.5 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4
4
Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm . This allows a wider variety of objects to be safely passed.
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

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

见实现注意事项。

元数据

  • 最后修改: