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).
Window: hashchange event
Syntax
Use the event name in methods like addEventListener()
, or set an event handler property.
addEventListener('hashchange', event => { }); onhashchange = event => { };
Event type
A HashChangeEvent
. Inherits from Event
.
Event properties
-
HashChangeEvent.newURL
Read only -
The new URL to which the window is navigating.
-
HashChangeEvent.oldURL
Read only -
The previous URL from which the window was navigated.
Event handler aliases
In addition to the Window
interface, the event handler property onhashchange
is also available on the following targets:
Examples
You can use the hashchange
event in an addEventListener
method:
window.addEventListener('hashchange', function() { 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
Specification |
---|
HTML Standard # event-hashchange |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
hashchange_event |
8
4-8
A regular
Event object is fired, rather than a
HashChangeEvent object.
|
12
|
3.6
|
8
|
10.6
|
5
|
≤37
|
18
|
4
|
11
|
5
|
1.0
|
See also
-
popstate
event
© 2005–2021 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