Window: hashchange event

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).

Bubbles Yes
Cancelable No
Interface HashChangeEvent
Event handler onhashchange


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;


HTML Standard (HTML)
# event-hashchange

Browser compatibility

BCD tables only load in the browser

See also