AI Workshop: learn to build apps with AI →
History API: The `popstate` event

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


This event fires on window when the user navigates the history (for example via the back or forward button). The event object has a state property with the state for the target entry:

window.onpopstate = event => {
  console.log(event.state)
}

or

window.addEventListener('popstate', event => {
  console.log(event.state)
})

The event’s state property is the state object (the first argument passed to pushState() or replaceState()). The event fires when you call history.back(), history.forward(), or history.go().

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
7: Bfcache and page restoration