HashChangeEvent

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The HashChangeEvent interface represents events that fire when the fragment identifier of the URL has changed.

The fragment identifier is the part of the URL that follows (and includes) the # symbol.

Event HashChangeEvent

Constructor

HashChangeEvent()

Creates a new HashChangeEvent object.

Instance properties

This interface also inherits the properties of its parent, Event.

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.

Instance methods

This interface has no methods of its own, but inherits the methods of its parent, Event.

Examples

Basic example

js
function locationHashChanged() {
  if (location.hash === "#some-cool-feature") {
    someCoolFeature();
  }
}

window.addEventListener("hashchange", locationHashChanged);

Specifications

Specification
HTML
# the-hashchangeevent-interface

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
HashChangeEvent
HashChangeEvent() constructor
newURL
oldURL

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support