This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The hashchange event is fired when the fragment identifier of the URL has changed (the part of the URL beginning with and following the # symbol).
This event does not fire when the hash is modified using history.pushState() or history.replaceState().
Use the event name in methods like addEventListener(), or set an event handler property.
addEventListener("hashchange", (event) => { })
onhashchange = (event) => { }
A HashChangeEvent. Inherits from Event.
HashChangeEvent.newURL Read only
A string representing the new URL the window is navigating to.
HashChangeEvent.oldURL Read only
A string representing the previous URL from which the window was navigated.
In addition to the Window interface, the event handler property onhashchange is also available on the following targets:
You can use the hashchange event in an addEventListener method:
window.addEventListener(
"hashchange",
() => {
console.log("The hash has changed!");
},
false,
);
Or use the onhashchange event handler property:
function locationHashChanged() {
if (location.hash === "#cool-feature") {
console.log("You're visiting a cool feature!");
}
}
window.onhashchange = locationHashChanged;
| Specification |
|---|
| HTML> # event-hashchange> |
| HTML> # handler-window-onhashchange> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
hashchange_event |
84–8A regularEvent object is fired, rather than a HashChangeEvent object. |
12 | 3.6 | 10.6 | 5 | 18 | 4 | 11 | 5 | 1.0 | 4.4 | 5 |
popstate event
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/hashchange_event