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/assign/index.html | 71 ++++++++ files/zh-cn/web/api/location/hash/index.html | 47 +++++ files/zh-cn/web/api/location/host/index.html | 52 ++++++ files/zh-cn/web/api/location/hostname/index.html | 43 +++++ files/zh-cn/web/api/location/href/index.html | 44 +++++ files/zh-cn/web/api/location/index.html | 219 +++++++++++++++++++++++ files/zh-cn/web/api/location/reload/index.html | 109 +++++++++++ files/zh-cn/web/api/location/replace/index.html | 71 ++++++++ files/zh-cn/web/api/location/search/index.html | 50 ++++++ files/zh-cn/web/api/location/tostring/index.html | 42 +++++ 10 files changed, 748 insertions(+) create mode 100644 files/zh-cn/web/api/location/assign/index.html create mode 100644 files/zh-cn/web/api/location/hash/index.html create mode 100644 files/zh-cn/web/api/location/host/index.html create mode 100644 files/zh-cn/web/api/location/hostname/index.html create mode 100644 files/zh-cn/web/api/location/href/index.html create mode 100644 files/zh-cn/web/api/location/index.html create mode 100644 files/zh-cn/web/api/location/reload/index.html create mode 100644 files/zh-cn/web/api/location/replace/index.html create mode 100644 files/zh-cn/web/api/location/search/index.html create mode 100644 files/zh-cn/web/api/location/tostring/index.html (limited to 'files/zh-cn/web/api/location') diff --git a/files/zh-cn/web/api/location/assign/index.html b/files/zh-cn/web/api/location/assign/index.html new file mode 100644 index 0000000000..8bf9c358f8 --- /dev/null +++ b/files/zh-cn/web/api/location/assign/index.html @@ -0,0 +1,71 @@ +--- +title: Location.assign() +slug: Web/API/Location/assign +tags: + - API + - Location + - 参考 + - 导航 + - 方法 + - 跳转 +translation_of: Web/API/Location/assign +--- +

{{ APIRef("HTML DOM") }}

+ +

Location.assign() 方法会触发窗口加载并显示指定的URL的内容。

+ +

如果由于安全原因无法执行跳转,那么会抛出一个 SECURITY_ERROR 类型的 {{domxref("DOMException")}}。当调用此方法的脚本来源和页面的 {{domxref("Location")}} 对象中定义的来源隶属于不同域的时候,就会抛出上述错误。

+ +

如果传入了一个无效的 URL,则会抛出一个 SYNTAX_ERROR 类型的 {{domxref("DOMException")}}。

+ +

语法

+ +
location.assign(url);
+
+ +

参数

+ +
+
url
+
一个包含了要跳转到的链接的{{domxref("DOMString")}}。
+
+ +

示例

+ +
// 跳转到 Location.reload() 这篇文章
+	document.location.assign('https://developer.mozilla.org/zh-CN/docs/Web/API/Location/reload');
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('HTML WHATWG', "history.html#dom-location-assign", "Location.assign()")}}{{Spec2('HTML WHATWG')}}和 {{SpecName("HTML5 W3C")}} 相同。
{{SpecName('HTML5 W3C', "browsers.html#dom-location-assign", "Location.assign()")}}{{Spec2('HTML5 W3C')}}第一次被定义。
+ +

浏览器兼容性

+ + + +

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

+ +

相关内容

+ + diff --git a/files/zh-cn/web/api/location/hash/index.html b/files/zh-cn/web/api/location/hash/index.html new file mode 100644 index 0000000000..0a0d45e220 --- /dev/null +++ b/files/zh-cn/web/api/location/hash/index.html @@ -0,0 +1,47 @@ +--- +title: 'Location: hash' +slug: Web/API/Location/hash +translation_of: Web/API/Location/hash +--- +
{{ APIRef("Location") }}
+ +

{{domxref("Location")}} 接口的 hash 属性返回一个 {{domxref("USVString")}},其中会包含URL标识中的 '#' 和 后面URL片段标识符。

+ +

这里 fragment 不会经过百分比编码(URL编码)。如果 URL 中没有 fragment,该属性会包含一个空字符串,""

+ +

Syntax

+ +
string = object.hash;
+object.hash = string;
+
+ +

Examples

+ +
<a id="myAnchor" href="/en-US/docs/Location.href#Examples">Examples</a>
+<script>
+  var anchor = document.getElementById("myAnchor");
+  console.log(anchor.hash); // 返回'#Examples'
+</script>
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-location-hash', 'hash')}}{{Spec2('HTML WHATWG')}}Initial definition
+ +

Browser compatibility

+ + + +

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

diff --git a/files/zh-cn/web/api/location/host/index.html b/files/zh-cn/web/api/location/host/index.html new file mode 100644 index 0000000000..f4ab84da3d --- /dev/null +++ b/files/zh-cn/web/api/location/host/index.html @@ -0,0 +1,52 @@ +--- +title: 'Location: host' +slug: Web/API/Location/host +translation_of: Web/API/Location/host +--- +
{{ApiRef("Location")}}
+ +

{{domxref("Location")}} 接口的 host 属性是包含了主机的一段 {{domxref("USVString")}},其中包含:主机名,如果 URL 的端口号是非空的,还会跟上一个 ':' ,最后是 URL 的端口号。

+ +

Syntax

+ +
string = object.host;
+object.host = string;
+
+ +

Examples

+ +
var anchor = document.createElement("a");
+
+anchor.href = "https://developer.mozilla.org/en-US/Location.host"
+anchor.host == "developer.mozilla.org"
+
+anchor.href = "https://developer.mozilla.org:443/en-US/Location.host"
+anchor.host == "developer.mozilla.org"
+// 这里 host 中没有包含端口号,因为 443 是 https协议的默认端口号
+
+anchor.href = "https://developer.mozilla.org:4097/en-US/Location.host"
+anchor.host == "developer.mozilla.org:4097"
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-location-host', 'host')}}{{Spec2('HTML WHATWG')}}Initial definition
+ +

Browser compatibility

+ + + +

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

diff --git a/files/zh-cn/web/api/location/hostname/index.html b/files/zh-cn/web/api/location/hostname/index.html new file mode 100644 index 0000000000..20c424dcdf --- /dev/null +++ b/files/zh-cn/web/api/location/hostname/index.html @@ -0,0 +1,43 @@ +--- +title: 'Location: hostname' +slug: Web/API/Location/hostname +translation_of: Web/API/Location/hostname +--- +

{{ApiRef("URL API")}}

+ +

{{domxref("Location")}}的 hostname 属性是包含了域名的一段 {{domxref("USVString")}}。

+ +

Syntax

+ +
string = object.hostname;
+object.hostname = string;
+
+ +

Examples

+ +
// 在文档流中声明了一个元素: <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/Location.hostname">
+var anchor = document.getElementById("myAnchor");
+var result = anchor.hostname; // Returns:'developer.mozilla.org'
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-location-hostname', 'hostname')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ + + +

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

diff --git a/files/zh-cn/web/api/location/href/index.html b/files/zh-cn/web/api/location/href/index.html new file mode 100644 index 0000000000..55b2cffb4f --- /dev/null +++ b/files/zh-cn/web/api/location/href/index.html @@ -0,0 +1,44 @@ +--- +title: 'Location: href' +slug: Web/API/Location/href +translation_of: Web/API/Location/href +--- +

{{ApiRef("Location")}}

+ +

{{domxref("Location")}} 接口的 href 属性是一个字符串化转换器(stringifier), 返回一个包含了完整 URL 的 {{domxref("USVString")}} 值, 且允许 href 的更新.

+ +

语法

+ +
string = object.href;
+object.href = string;
+
+ +

范例

+ +
// 假设文档中包含标签: <a id="myAnchor" href="https://developer.mozilla.org/en-US/Location/href">
+var anchor = document.getElementById("myAnchor");
+var result = anchor.href; // 返回: 'https://developer.mozilla.org/en-US/Location/href'
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-location-href', 'href')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

浏览器兼容性

+ + + +

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

diff --git a/files/zh-cn/web/api/location/index.html b/files/zh-cn/web/api/location/index.html new file mode 100644 index 0000000000..7ab82cf7fa --- /dev/null +++ b/files/zh-cn/web/api/location/index.html @@ -0,0 +1,219 @@ +--- +title: Location +slug: Web/API/Location +tags: + - API + - Interface + - Location +translation_of: Web/API/Location +--- +

{{APIRef("URLUtils")}}

+ +

Location 接口表示其链接到的对象的位置(URL)。所做的修改反映在与之相关的对象上。 {{domxref("Document")}} 和 {{domxref("Window")}} 接口都有这样一个链接的Location,分别通过 {{domxref("Document.location")}}和{{domxref("Window.location")}} 访问。

+ +

属性

+ +

Location 接口不继承任何属性,但是实现了那些来自 {{domxref("URLUtils")}} 的属性。

+ +
+
{{domxref("Location.href")}}
+
包含整个URL的一个{{domxref("DOMString")}}
+
{{domxref("Location.protocol")}}
+
包含URL对应协议的一个{{domxref("DOMString")}},最后有一个":"。
+
{{domxref("Location.host")}}
+
包含了域名的一个{{domxref("DOMString")}},可能在该串最后带有一个":"并跟上URL的端口号。
+
{{domxref("Location.hostname")}}
+
包含URL域名的一个{{domxref("DOMString")}}。
+
{{domxref("Location.port")}}
+
包含端口号的一个{{domxref("DOMString")}}。
+
{{domxref("Location.pathname")}}
+
包含URL中路径部分的一个{{domxref("DOMString")}},开头有一个“/"。
+
{{domxref("Location.search")}}
+
 包含URL参数的一个{{domxref("DOMString")}},开头有一个“?”
+
{{domxref("Location.hash")}}
+
包含块标识符的{{domxref("DOMString")}},开头有一个“#”。
+
{{domxref("Location.username")}}
+
包含URL中域名前的用户名的一个{{domxref("DOMString")}}。
+
{{domxref("Location.password")}}
+
包含URL域名前的密码的一个 {{domxref("DOMString")}}。
+
{{domxref("Location.origin")}} {{readOnlyInline}}
+
包含页面来源的域名的标准形式{{domxref("DOMString")}}。
+
+ +

方法

+ +

Location没有继承任何方法,但实现了来自{{domxref("URLUtils")}}的方法。

+ +
+
{{domxref("Location.assign()")}}
+
加载给定URL的内容资源到这个Location对象所关联的对象上。
+
{{domxref("Location.reload()")}}
+
重新加载来自当前 URL的资源。他有一个特殊的可选参数,类型为 {{domxref("Boolean")}},该参数为true时会导致该方法引发的刷新一定会从服务器上加载数据。如果是 false或没有制定这个参数,浏览器可能从缓存当中加载页面。
+
{{domxref("Location.replace()")}}
+
用给定的URL替换掉当前的资源。与 assign() 方法不同的是用 replace()替换的新页面不会被保存在会话的历史 {{domxref("History")}}中,这意味着用户将不能用后退按钮转到该页面。
+
{{domxref("Location.toString()")}}
+
返回一个{{domxref("DOMString")}},包含整个URL。 它和读取{{domxref("URLUtils.href")}}的效果相同。但是用它是不能够修改Location的值的。
+
+ +

例子

+ +
// Create anchor element and use href property for the purpose of this example
+// A more correct alternative is to browse to the URL and use document.location or window.location
+var url = document.createElement('a');
+url.href = 'https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container';
+console.log(url.href);      // https://developer.mozilla.org/en-US/search?q=URL#search-results-close-container
+console.log(url.protocol);  // https:
+console.log(url.host);      // developer.mozilla.org
+console.log(url.hostname);  // developer.mozilla.org
+console.log(url.port);      // (blank - https assumes port 443)
+console.log(url.pathname);  // /en-US/search
+console.log(url.search);    // ?q=URL
+console.log(url.hash);      // #search-results-close-container
+console.log(url.origin);    // https://developer.mozilla.org
+
+ +

规范

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

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
特性ChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
origin on Windows.location{{CompatUnknown}}{{CompatGeckoDesktop("21")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
origin on all location objects (but on Workers, that use {{domxref("WorkerLocation")}}{{CompatUnknown}}{{CompatGeckoDesktop("26")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
username and password{{CompatUnknown}}{{CompatGeckoDesktop("26")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
searchParams{{CompatUnknown}}{{CompatGeckoDesktop("34")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
origin on Windows.location{{CompatUnknown}}{{CompatGeckoMobile("21")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
origin on all location objects (but on Workers, that use {{domxref("WorkerLocation")}}){{CompatUnknown}}{{CompatGeckoMobile("26")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
username and password{{CompatUnknown}}{{CompatGeckoMobile("26")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
searchParams{{CompatUnknown}}{{CompatGeckoMobile("34")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

另见

+ + + +
+
 
+
diff --git a/files/zh-cn/web/api/location/reload/index.html b/files/zh-cn/web/api/location/reload/index.html new file mode 100644 index 0000000000..482a779a89 --- /dev/null +++ b/files/zh-cn/web/api/location/reload/index.html @@ -0,0 +1,109 @@ +--- +title: Location.reload() +slug: Web/API/Location/reload +translation_of: Web/API/Location/reload +--- +

{{ APIRef("HTML DOM") }}

+ +

Location.reload() 方法用来刷新当前页面。该方法只有一个参数,当值为 true 时,将强制浏览器从服务器加载页面资源,当值为 false 或者未传参时,浏览器则可能从缓存中读取页面。

+ +

该方法在跨域调用(执行该方法的脚本文件的域和 {{domxref("Location")}} 对象所在页面的跨不同)时,将会抛出 {{domxref("DOMException")}} 异常。

+ +

语法

+ +
object.reload(forcedReload);
+
+ +

参数

+ +
+
forcedReload {{optional_inline}}
+
该参数要求为 {{domxref("Boolean","布尔")}} 类型,当取值为 true 时,将强制浏览器从服务器重新获取当前页面资源,而不是从浏览器的缓存中读取,如果取值为 false 或不传该参数时,浏览器则可能会从缓存中读取当前页面。
+
+ +

示例

+ +
// 无缓存刷新页面(但页面引用的资源还是可能使用缓存,
+// 大多数浏览器可以通过设置在打开开发者工具时禁用缓存实现无缓存需求)
+window.location.reload(true);
+ +

规范

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

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

相关链接

+ + 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")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/location/search/index.html b/files/zh-cn/web/api/location/search/index.html new file mode 100644 index 0000000000..deb9fe589a --- /dev/null +++ b/files/zh-cn/web/api/location/search/index.html @@ -0,0 +1,50 @@ +--- +title: 'Location: search' +slug: Web/API/Location/search +translation_of: Web/API/Location/search +--- +
{{ApiRef("Location")}}
+ +

{{domxref("Location")}} 接口的 search 属性会返回一段 {{domxref("USVString")}},其中包含一个URL标识中的 '?' 以及跟随其后的一串URL查询参数。

+ +

现代浏览器提供 URLSearchParams 和 URL.searchParams 两个接口,使得从查询字符串中解析出查询参数变得更加容易。

+ +

Syntax

+ +
string = object.search;
+object.search = string;
+
+ +

Examples

+ +
// 声明了一个 <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/Location.search?q=123"> 元素在文档流中
+var anchor = document.getElementById("myAnchor");
+var queryString = anchor.search; // Returns:'?q=123'
+
+// 进一步解析:
+let params = new URLSearchParams(queryString);
+let q = parseInt(params.get("q")); // is the number 123
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('HTML WHATWG', '#dom-location-search', 'search')}}{{Spec2('HTML WHATWG')}}Initial definition.
+ +

Browser compatibility

+ + + +

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

diff --git a/files/zh-cn/web/api/location/tostring/index.html b/files/zh-cn/web/api/location/tostring/index.html new file mode 100644 index 0000000000..84a5fdddff --- /dev/null +++ b/files/zh-cn/web/api/location/tostring/index.html @@ -0,0 +1,42 @@ +--- +title: 'Location: toString()' +slug: Web/API/Location/toString +translation_of: Web/API/Location/toString +--- +

{{ApiRef(“ Location”)}}

+ +

toString(){{domxref(“ Location”)}}接口stringifier方法返回包含整个URL的{{domxref(“ USVString”)}}}。它是{{domxref(“ Location.href”)}}的只读版本。

+ +

句法

+ +
string = object.toString();
+ +

例子

+ +
// Let's imagine an <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/Location/toString"> element is in the document
+var anchor = document.getElementById("myAnchor");
+var result = anchor.toString(); // Returns: 'https://developer.mozilla.org/en-US/docs/Location/toString'
+
+ +

技术指标

+ + + + + + + + + + + + + + +
规范状态评论
{{SpecName('HTML WHATWG',“#dom-location-href”)}}{{Spec2('HTML WHATWG')}}
+ +

浏览器兼容性

+ + + +

{{Compat(“ api.Location.toString”)}}

-- cgit v1.2.3-54-g00ecf