From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/location/replace/index.html | 71 +++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 files/zh-cn/web/api/location/replace/index.html (limited to 'files/zh-cn/web/api/location/replace/index.html') diff --git a/files/zh-cn/web/api/location/replace/index.html b/files/zh-cn/web/api/location/replace/index.html new file mode 100644 index 0000000000..410036a441 --- /dev/null +++ b/files/zh-cn/web/api/location/replace/index.html @@ -0,0 +1,71 @@ +--- +title: Location.replace() +slug: Web/API/Location/replace +tags: + - API + - DOM + - Location + - 参考 + - 导航 + - 方法 +translation_of: Web/API/Location/replace +--- +

{{ APIRef("Location") }}

+ +

Location.replace() 方法以给定的URL来替换当前的资源。 与{{domxref("Location.assign","assign()")}} 方法 不同的是,调用 replace() 方法后,当前页面不会保存到会话历史中(session {{domxref("History")}}),这样,用户点击回退按钮时,将不会再跳转到该页面。

+ +

因违反安全规则导致的赋值失败,浏览器将会抛出类型为 SECURITY_ERROR 的 {{domxref("DOMException")}} 异常。当调用该方法的脚本所属的源与拥有 {{domxref("Location")}} 对象所属源不同时,通常情况会发生这种异常,此时通常该脚本是存在不同的域下。

+ +

如果 URL 无效,浏览器也会抛出 SYNTAX_ERROR 类型的 {{domxref("DOMException")}} 异常。

+ +

语法

+ +
object.replace(url);
+
+ +

参数

+ +
+
url
+
 {{domxref("DOMString")}} 类型,指定所导航到的页面的 URL 地址。
+
+ +

示例

+ +
// Navigate to the Location.reload article by replacing this page
+window.location.replace('https://developer.mozilla.org/en-US/docs/Web/API/Location/reload');
+ +

标准

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', "history.html#dom-location-replace", "Location.replace()")}}{{Spec2('HTML WHATWG')}}No change from {{SpecName("HTML5 W3C")}}.
{{SpecName('HTML5 W3C', "browsers.html#dom-location-replace", "Location.replace()")}}{{Spec2('HTML5 W3C')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.Location.replace")}}

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf