aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/window/onhashchange/index.html
blob: 0c7f3ebefa43410cbc7892d1008905ef078f42ce (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
117
118
119
120
121
122
123
124
---
title: window.onhashchange
slug: Web/API/Window/onhashchange
tags:
  - HTML-DOM
  - Property
  - Reference
  - WindowEventHandlers
translation_of: Web/API/WindowEventHandlers/onhashchange
---
<p>{{APIRef("HTML DOM")}}</p>

<p>当 一个窗口的 hash (URL 中 # 后面的部分)改变时就会触发 <strong>hashchange </strong>事件(参见 {{domxref("Window.location", "location.hash")}})。</p>

<h2 class="editable" id="语法"><span>语法</span></h2>

<pre class="syntaxbox">window.onhashchange = funcRef;
</pre>

<p>或者</p>

<pre class="syntaxbox">&lt;body onhashchange="funcRef();"&gt;</pre>

<p>以上操作将覆盖现有的事件处理程序。</p>

<p>为了添加一个新的事件处理程序,而不覆盖掉已有的其他事件处理程序,可以使用函数 <strong>"addEventListener"</strong></p>

<pre class="syntaxbox">window.addEventListener("hashchange", funcRef, false);
</pre>

<h3 id="参数"><span>参数</span></h3>

<dl>
 <dt><code>funcRef</code></dt>
 <dd>对一个函数的引用。</dd>
</dl>

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

<pre class="brush: js">if ("onhashchange" in window) {
    alert("该浏览器支持 hashchange 事件!");
}

function locationHashChanged() {
    if (location.hash === "#somecoolfeature") {
        somecoolfeature();
    }
}

window.onhashchange = locationHashChanged;
</pre>

<h2 class="editable" id="hashchange_事件">hashchange 事件</h2>

<p><code>hashchange</code> 事件对象有下面两个属性:</p>

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">属性</td>
   <td class="header">类型</td>
   <td class="header">描述</td>
  </tr>
  <tr>
   <td><code>newURL</code> {{ gecko_minversion_inline("6.0") }}</td>
   <td><code>DOMString</code></td>
   <td>当前页面新的URL</td>
  </tr>
  <tr>
   <td><code>oldURL</code> {{ gecko_minversion_inline("6.0") }}</td>
   <td><code>DOMString</code></td>
   <td>当前页面旧的URL</td>
  </tr>
 </tbody>
</table>

<h3 id="Workaround_for_event.newURL_and_event.oldURL">Workaround for event.newURL and event.oldURL</h3>

<pre class="brush: js">//let this snippet run before your hashchange event binding code
if(!window.HashChangeEvent)(function(){
	var lastURL=document.URL;
	window.addEventListener("hashchange",function(event){
		Object.defineProperty(event,"oldURL",{enumerable:true,configurable:true,value:lastURL});
		Object.defineProperty(event,"newURL",{enumerable:true,configurable:true,value:document.URL});
		lastURL=document.URL;
	});
}());</pre>

<h2 id="规范">规范</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('HTML WHATWG', '#windoweventhandlers', 'GlobalEventHandlers')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', '#windoweventhandlers', 'GlobalEventHandlers')}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5 W3C", "#windoweventhandlers", "GlobalEventHandlers")}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

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

<p>{{Compat("api.WindowEventHandlers.onhashchange")}}</p>

<ul>
 <li><a href="/en-US/docs/DOM/Manipulating_the_browser_history" title="DOM/Manipulating the browser history">Manipulating the browser history</a>, <a href="/en-US/docs/DOM/window.history" title="DOM/window.history">history.pushState() and history.replaceState()</a> methods, <a href="/en-US/docs/DOM/window.onpopstate" title="DOM/window.onpopstate">popstate</a> event.</li>
</ul>