Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Window: hashchange event

BaselineWidely available

Thehashchange 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 usinghistory.pushState() orhistory.replaceState().

Syntax

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

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

Event type

Event properties

HashChangeEvent.newURLRead only

A string representing the new URL the window is navigating to.

HashChangeEvent.oldURLRead only

A string representing the previous URL from which the window was navigated.

Event handler aliases

In addition to theWindow interface, the event handler propertyonhashchange is also available on the following targets:

Examples

You can use thehashchange event in anaddEventListener method:

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

Or use theonhashchange 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

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp