Window: hashchange イベント

hashchange イベントは、 URL のフラグメント識別子 (URL の # 記号で始まり続く部分) が変化したときに発生します。

バブリング あり
キャンセル 不可
インターフェイス HashChangeEvent (en-US)
イベントハンドラープロパティ onhashchange

hashchange イベントは addEventListener メソッドの中で使用することができます。

window.addEventListener('hashchange', function() {
  console.log('The hash has changed!')
}, false);

または onhashchange イベントハンドラープロパティを使用して、

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

window.onhashchange = locationHashChanged;

仕様書

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

ブラウザーの互換性

BCD tables only load in the browser

関連情報