Baseline Widely available
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) => { }
Event type Event properties
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;
Specifications Browser compatibility See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3