From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../zh-cn/web/api/history/replacestate/index.html | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 files/zh-cn/web/api/history/replacestate/index.html (limited to 'files/zh-cn/web/api/history/replacestate/index.html') diff --git a/files/zh-cn/web/api/history/replacestate/index.html b/files/zh-cn/web/api/history/replacestate/index.html new file mode 100644 index 0000000000..e2ff2e1311 --- /dev/null +++ b/files/zh-cn/web/api/history/replacestate/index.html @@ -0,0 +1,66 @@ +--- +title: History.replaceState() +slug: Web/API/History/replaceState +translation_of: Web/API/History/replaceState +--- +

{{APIRef("DOM")}}

+ +

replaceState()方法使用state objects, title,和 URL 作为参数, 修改当前历史记录实体,如果你想更新当前的state对象或者当前历史实体的URL来响应用户的的动作的话这个方法将会非常有用。

+ +

语法

+ +
history.replaceState(stateObj, title[, url]);
+ +

参数

+ +
+
stateObj
+
状态对象是一个JavaScript对象,它与传递给 replaceState 方法的历史记录实体相关联.
+
title
+
大部分浏览器忽略这个参数, 将来可能有用. 在此处传递空字符串应该可以防止将来对方法的更改。或者,您可以为该状态传递简短标题
+
url {{optional_inline}}
+
历史记录实体的URL. 新的URL跟当前的URL必须是同源; 否则 replaceState 抛出一个异常.
+
+ +

例子

+ +

假设 http://mozilla.org/foo.html 执行下面的 JavaScript 代码:

+ +
var stateObj = { foo: "bar" };
+history.pushState(stateObj, "", "bar.html");
+ +

上面这两行的解释可以在 "Example of pushState() method"这个章节找到。然后假设 http://mozilla.org/bar.html  执行下面的 JavaScript 代码:

+ +
history.replaceState(stateObj, "", "bar2.html");
+ +

这会让URL栏显示 http://mozilla.org/bar2.html, 但是不会刷新 bar2.html  页面 甚至不会检查bar2.html 是否存在

+ +

假设用户跳转到 http://www.microsoft.com, 然后点击返回按钮.这时, URL 栏将会显示 http://mozilla.org/bar2.html 页面. 如果用户此时点击返回按钮, URL栏将会显示 http://mozilla.org/foo.html 页面, 最终绕过了 bar.html 页面.

+ +

说明

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("HTML WHATWG", "browsers.html#history", "History")}}{{Spec2("HTML WHATWG")}}No change from {{SpecName("HTML5 W3C")}}.
{{SpecName("HTML5 W3C", "browsers.html#history", "History")}}{{Spec2("HTML5 W3C")}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.History.replaceState")}}

-- cgit v1.2.3-54-g00ecf