The hashchange event fires when a window's hash changes (see location.hash
).
Syntax
window.onhashchange = funcRef;
or
<body onhashchange="funcRef();">
or
window.addEventListener("hashchange", funcRef, false);
Parameters
funcRef
- 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;
The hashchange event
The dispatched hashchange
event has the following fields:
Field | Type | Description |
newURL |
DOMString |
The new URL to which the window is navigating. |
oldURL |
DOMString |
The previous URL from which the window was navigated. |
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'GlobalEventHandlers' in that specification. |
Living Standard | |
HTML5.1 The definition of 'GlobalEventHandlers' in that specification. |
Working Draft | |
HTML5 The definition of 'GlobalEventHandlers' in that specification. |
Recommendation |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5.0 | 3.6 (1.9.2) |
8.0
|
10.6 | 5.0 |
Document Tags and Contributors
Tags:
Contributors to this page:
teoli,
Taken,
libreGeekingKid,
leon_gilyadov,
Sheppy,
ethertank,
ziyunfei,
McGurk,
Jürgen Jeka,
Crash,
Nickolay,
jlebar,
Sephr
Last updated by:
teoli,