History API: The `hashchange` event

This event is called on window every time the fragment identifier of the URL changes:

window.onhashchange = event => {
  console.log(location.hash)
}

or

window.addEventListener('hashchange', event => {
  console.log(location.hash)
})

Lessons in this unit:

0: Introduction
1: Navigating the history
2: Add an entry to the history
3: Modify history entries
4: Access the current history entry state
5: The `popstate` event
6: ▶︎ The `hashchange` event
Want to learn more? Check out our courses