Table of contents
- 1. Syntax
- 2. Parameters
- 3. Example
- 4. The hashchange event
- 5. Browser compatibility
- 6. Specification
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
funcRefis 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 Requires Gecko 6.0
| DOMString | The new URL to which the window is navigating. |
oldURL Requires Gecko 6.0
| DOMString | The previous URL from which the window was navigated. |
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 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | 2.2 | 1.0 (1.9.2) | 9.0 | 11.0 | 5.0 |
Mozilla Developer Network