Window.onhashchange

  • Revision slug: DOM/window.onhashchange
  • Revision title: window.onhashchange
  • Revision id: 30973
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 2 words added, 1 words removed

Revision Content

{{ DomRef() }}

{{ gecko_minversion_header("1.9.2") }}

The hashchange event fires when a window's hash changes (see location.hash).

{{ gecko_minversion_note("1.9.3", "Starting in " .. {{gecko("1.9.3")}} .. ", this event is dispatched synchronously instead of asynchronously, as per the latest version of the HTML5 specification.") }}

Syntax

window.onhashchange = funcRef;

or

<body onhashchange="funcRef();">

or

window.addEventListener("hashchange", funcRef, false);

Parameters

  • funcRef is a reference to a function.

Example

if ("onhashchange" in window) {
    alert("The browser supports the hashchange event!");
}

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

window.onhashchange = locationHashChanged;

Other Browsers

Internet Explorer 8 also supports the hashchange event.  See this page for details.

See Also

  • Bug 385434 - Add support for HTML5 onhashchange (event for named anchor changes)
  • Bug 504837 - hashchange event should not be restricted to firing only when the document's ready state is "complete"
  • Bug 504220 - document.body and window should contain onhashchange attribute

Specification

Revision Source

<p>{{ DomRef() }}</p>
<p>{{ gecko_minversion_header("1.9.2") }}</p>
<p>The <strong>hashchange</strong> event fires when a window's hash changes (see <a href="/en/DOM/window.location" title="en/DOM/window.location">location.hash</a>).</p>
<p>{{ gecko_minversion_note("1.9.3", "Starting in " .. {{gecko("1.9.3")}} .. ", this event is dispatched synchronously instead of asynchronously, as per the latest version of the HTML5 specification.") }}</p>
<h3 class="editable"><span>Syntax </span></h3>
<pre>window.onhashchange = funcRef;
</pre>
<p><strong>or</strong></p>
<pre>&lt;body onhashchange="funcRef();"&gt;
</pre>
<p><strong>or</strong></p>
<pre>window.addEventListener("hashchange", funcRef, false);
</pre>
<h3 class="editable"><span>Parameters </span></h3>
<ul> <li><code>funcRef</code> is a reference to a function.</li>
</ul>
<h3 class="editable"><span>Example</span></h3>
<pre>if ("onhashchange" in window) {
    alert("The browser supports the hashchange event!");
}

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

window.onhashchange = locationHashChanged;
</pre>
<h3 class="editable">Other Browsers</h3>
<p>Internet Explorer 8 also supports the <code>hashchange</code> event.  See <a class="external" href="http://msdn.microsoft.com/en-us/library/cc288209(VS.85).aspx" title="http://msdn.microsoft.com/en-us/library/cc288209(VS.85).aspx">this page</a> for details.</p>
<h3 class="editable"><span>See Also</span></h3>
<ul> <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=385434" title="https://bugzilla.mozilla.org/show_bug.cgi?id=385434">Bug 385434</a> - Add support for HTML5 onhashchange (event for named anchor changes)</li> <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=504837" title="https://bugzilla.mozilla.org/show_bug.cgi?id=504837">Bug 504837</a> - <span class="bz_default_hidden" id="summary_alias_container" style="display: inline;"><span id="short_desc_nonedit_display">hashchange event should not be restricted to firing only when the document's ready state is "complete"</span></span></li> <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=504220" title="https://bugzilla.mozilla.org/show_bug.cgi?id=504220">Bug 504220</a> - <span class="bz_default_hidden" id="summary_alias_container" style="display: inline;"><span id="short_desc_nonedit_display">document.body and window should contain onhashchange attribute</span></span></li>
</ul>
<h3 class="editable"><span>Specification</span></h3>
<ul> <li><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/#handler-window-onhashchange" title="http://www.whatwg.org/specs/web-apps/current-work/#handler-window-onhashchange"><span>http://www.whatwg.org/specs/web-apps/current-work/#handler-window-onhashchange</span></a></li>
</ul>
Revert to this revision