blob: 5b383f066fe1e183dd91a41ad45039d29ad1a3a7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
---
title: Location
slug: Web/API/Location
tags:
- API
- Interface
- Location
translation_of: Web/API/Location
---
<p>{{APIRef("URLUtils")}}</p>
<p><strong><code>Location </code></strong>接口表示其链接到的对象的位置(URL)。所做的修改反映在与之相关的对象上。 {{domxref("Document")}} 和 {{domxref("Window")}} 接口都有这样一个链接的Location,分别通过 {{domxref("Document.location")}}和{{domxref("Window.location")}} 访问。</p>
<h2 id="属性">属性</h2>
<p><em><code>Location </code>接口</em><em>不继承任何属性,但是实现了那些来自 {{domxref("URLUtils")}} 的属性。</em></p>
<dl>
<dt>{{domxref("Location.href")}}</dt>
<dd>包含整个URL的一个{{domxref("DOMString")}}</dd>
<dt>{{domxref("Location.protocol")}}</dt>
<dd>包含URL对应协议的一个{{domxref("DOMString")}},最后有一个":"。</dd>
<dt>{{domxref("Location.host")}}</dt>
<dd>包含了域名的一个{{domxref("DOMString")}},可能在该串最后带有一个":"并跟上URL的端口号。</dd>
<dt>{{domxref("Location.hostname")}}</dt>
<dd>包含URL域名的一个{{domxref("DOMString")}}。</dd>
<dt>{{domxref("Location.port")}}</dt>
<dd>包含端口号的一个{{domxref("DOMString")}}。</dd>
<dt>{{domxref("Location.pathname")}}</dt>
<dd>包含URL中路径部分的一个{{domxref("DOMString")}},开头有一个“<code>/"。</code></dd>
<dt>{{domxref("Location.search")}}</dt>
<dd> 包含URL参数的一个{{domxref("DOMString")}},开头有一个<code>“?”</code>。</dd>
<dt>{{domxref("Location.hash")}}</dt>
<dd>包含块标识符的{{domxref("DOMString")}},开头有一个<code>“#”。</code></dd>
<dt>{{domxref("Location.username")}}</dt>
<dd>包含URL中域名前的用户名的一个{{domxref("DOMString")}}。</dd>
<dt>{{domxref("Location.password")}}</dt>
<dd>包含URL域名前的密码的一个 {{domxref("DOMString")}}。</dd>
<dt>{{domxref("Location.origin")}} {{readOnlyInline}}</dt>
<dd>包含页面来源的域名的标准形式{{domxref("DOMString")}}。</dd>
</dl>
<h2 id="方法">方法</h2>
<p><em><code>Location</code></em><em>没有继承任何方法<em>,但实现了来自{{domxref("URLUtils")}}</em>的方法。</em></p>
<dl>
<dt>{{domxref("Location.assign()")}}</dt>
<dd>加载给定URL的内容资源到这个Location对象所关联的对象上。</dd>
<dt>{{domxref("Location.reload()")}}</dt>
<dd>重新加载来自当前 URL的资源。他有一个特殊的可选参数,类型为 {{domxref("Boolean")}},该参数为true时会导致该方法引发的刷新一定会从服务器上加载数据。如果是 <code>false</code>或没有制定这个参数,浏览器可能从缓存当中加载页面。</dd>
<dt>{{domxref("Location.replace()")}}</dt>
<dd>用给定的URL替换掉当前的资源。与 <code>assign()</code> 方法不同的是用 <code>replace()</code>替换的新页面不会被保存在会话的历史 {{domxref("History")}}中,这意味着用户将不能用后退按钮转到该页面。</dd>
<dt>{{domxref("Location.toString()")}}</dt>
<dd>返回一个{{domxref("DOMString")}},包含整个URL。 它和读取{{domxref("URLUtils.href")}}的效果相同。但是用它是不能够修改Location的值的。</dd>
</dl>
<h2 id="例子">例子</h2>
<pre class="brush: js">// 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
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG', "history.html#the-location-interface", "Location")}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td>No change from {{SpecName("HTML5 W3C")}}.</td>
</tr>
<tr>
<td>{{SpecName('HTML5 W3C', "browsers.html#the-location-interface", "Location")}}</td>
<td>{{Spec2('HTML5 W3C')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
{{Compat("api.Location")}}
<h2 id="另见">另见</h2>
<ul>
<li>Two methods creating such an object: {{domxref("Window.location")}} and {{domxref("Document.location")}}.</li>
</ul>
<dl>
<dt> </dt>
</dl>
|