AI Workshop: learn to build apps with AI →
History API: Navigating the history

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


The History API provides these methods:

  • back()
  • forward()
  • go()

To go back to the previous page:

history.back()

This navigates to the previous entry in the session history. To go forward to the next page:

history.forward()

This is the same as using the browser back and forward buttons.

go() lets you move back or forward by multiple steps. For example:

history.go(-1) // equivalent to history.back()
history.go(-2) // equivalent to calling history.back() twice
history.go(1) // equivalent to history.forward()
history.go(3) // same as calling history.forward() three times

To get the number of entries in the history:

history.length

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