Window: hashchange event

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

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("hashchange", (event) => { })

onhashchange = (event) => { }

Event type

A HashChangeEvent. Inherits from Event.

Event HashChangeEvent

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.

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:

js
window.addEventListener("hashchange", () => {
  console.log("The hash has changed!");
});

Or use the onhashchange event handler property:

js
function locationHashChanged() {
  if (location.hash === "#cool-feature") {
    console.log("You're visiting a cool feature!");
  }
}

window.onhashchange = locationHashChanged;

Specifications

Specification
HTML
# event-hashchange
HTML
# handler-window-onhashchange

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
hashchange event
Chrome – No support
Chrome 4 – 7 (Release date: 2010-01-25)
footnote A regular Event object is fired, rather than a HashChangeEvent object.
Chrome – Full support
Chrome 8 (Release date: 2010-12-02)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 3.6 (Release date: 2010-01-21)
footnote Full support
Opera – Full support
Opera 10.6 (Release date: 2010-07-01)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 11 (Release date: 2011-03-22)
footnote Full support
Safari on iOS – Full support
Safari on iOS 5 (Release date: 2011-10-12)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 5 (Release date: 2011-10-12)
footnote Full support
Deno – No support
Deno
footnote No support

Legend

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

Full support
Full support
No support
No support
Has more compatibility info.

See also