Web History API

The Web History API provides simple methods to access the windows.history object.

The window.history object contains the URLs (websites) visited by the user.

All browsers support the Web History API:

Chrome IE Firefox Safari Opera
Supports Supports Supports Supports Supports

History back() Method

The back() method loads the previous URL from the windows.history list.

This is the same as clicking the 'back arrow' in the browser.

Example

<button onclick="myFunction()">Back</button>
<script>
function myFunction() {
  window.history.back();
}
</script>

History go() Method

The go() method loads a specific URL from the history list:

Example

<button onclick="myFunction()">Go back two pages</button>
<script>
function myFunction() {
  window.history.go(-2);
}
</script>

History Object Properties

Attribute Description
length Returns the number of URLs in the history list.

History Object Methods

Method Description
back() Load the previous URL in the history list.
forward() Load the next URL in the history list.
go() Load a specific URL from the history list.