Browser History

JavaScript can access the history of the web browser through the history object.

The history object holds information about the URLs which have been visited both before and after the current one.

Using the history object the current page can be changed, single stepping backwards and forwards through this list, and jump to any point.

History Object Properties

The table below lists the history object properties of interest.

Property Description
history.length keeps track of the length of the history list.
history.current is the value of the current history entry.
history.next is the next entry in the list. This is equivalent to pressing the forward button on the browser. It is therefore quire likely that it will have no value.
history.previous is the page before, equivalent to pressing the back button on the browser.

The history list may also be treated as an array. So we can access history[0].

Examples

To step one page backwards in the history
window.history.back();
or
window.history.go(-1);

Similarly to step forward a page

window.history.go(1);

History Object Methods

History object methods allow actions to be taken, similar to pushing the browser forward or backward buttons a number of times.

Method Description
history.forward the same as history.next
history.back the same as history.previous
history.go the equivalent to pushing either the forward or back button a number of times. For example history.go(-3) is equivalent to pressing the back button 3 times.