aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/htmlanchorelement/referrer/index.html
blob: b3e30b3fe27a9df6b5bed52415e26d7f882bc476 (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
110
111
112
113
114
115
116
---
title: HTMLAnchorElement.referrer
slug: Web/API/HTMLAnchorElement/referrer
translation_of: Web/API/HTMLAnchorElement/referrerPolicy
---
<div>{{APIRef}}{{SeeCompatTable}}</div>

<p><code><strong>HTMLAnchorElement</strong></code><strong><code>.referrer</code></strong> 属性对应于 HTML 中 {{HTMLElement("a")}} 标签的 {{htmlattrxref("referrer","a")}} 属性,它可以控制用户在点击这个链接时所发出的 HTTP 请求的 Referer 请求头的值。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>refStr</var> = <var>anchorElt</var>.referrer;
<var>anchorElt</var>.referrer = <var>refStr</var>;</pre>

<h3 id="属性值">属性值</h3>

<dl>
 <dd>
 <ul>
  <li><code>"no-referrer"</code> 意味着不要发送 Referer 请求头。</li>
  <li><code>"origin"</code>  意味着所发送的 Referer 请求头的值为当前页面的源,即 <code>location.origin</code> 的值。</li>
  <li><code>"unsafe-url"</code> 意味着所发送的 Referrer 请求头的值为当前页面完整的 url(即<code> location.href</code>)去掉尾部的哈希(即 <code>location.hash</code>)之后的值。正如该选项的名字所言(unsafe),此选项是不安全的,它可以将一个 HTTPS 页面的路径信息透露给第三方。</li>
 </ul>
 </dd>
</dl>

<h2 id="示例">示例</h2>

<pre class="brush: js">var elt = document.createElement("a");
var linkText = document.createTextNode("My link");
elt.appendChild(linkText);
elt.href = "https://developer.mozilla.org/en-US/";
elt.referrer = "no-referrer";

var div = document.getElementById("divAround");
div.appendChild(elt); // 点击该链接接时不会发送 Referer 请求头
</pre>

<h2 id="Specifications" name="Specifications">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('Referrer Policy', '#referrer-policy-delivery-referrer-attribute', 'referrer attribute')}}</td>
   <td>{{Spec2('Referrer Policy')}}</td>
   <td>Added the <code>referrer</code> attribute.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

<div>{{CompatibilityTable}}
<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("42.0")}} [1]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("42.0")}} [1]</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] 该特性目前默认为关闭状态,请通过将 <code>network.http.enablePerElementReferrer</code> 选项设置为 <code>true</code> 来开启。</p>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{domxref("HTMLImageElement.referrer")}}{{domxref("HTMLAreaElement.referrer")}}{{domxref("HTMLIFrameElement.referrer")}}</li>
</ul>