aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html')
-rw-r--r--files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html124
1 files changed, 124 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html b/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html
new file mode 100644
index 0000000000..0c7f3ebefa
--- /dev/null
+++ b/files/zh-cn/web/api/windoweventhandlers/onhashchange/index.html
@@ -0,0 +1,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>