English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JS Window History

The window.history object contains the browser's history.

history object

window.historyThe object contains the list of all pages visited in the current frame or window, which is part of the browser session history.

window.historyYou can write an object without a window prefix.

Some examples:

The next section will show you how to get information about the user's browsing history.

However, in order to protect user privacy, there are some restrictions on how JavaScript accesses this object.

Get the number of visited pages

Thehistory.lengthThe property returns the number of pages in the current window of the browser session history.

It also includes the current loaded page.

var result = history.length; // Returns the size of the current session history.
Test to see‹/›

You can use this property to find out how many pages the user has visited during the current browser session.

return to the previous page

Thehistory.back()The method will load the previous URL from the history list.

This is the same as clicking the 'Back' button in the browser.

<button onclick="history.back();">Back</button>
Test to see‹/›

The following output will be displayed by the above code:

go to the next page

Thehistory.forward()The method will load the next URL in the history list.

This is the same as clicking the 'Forward' button in the browser.

<button onclick="history.forward();">Forward</button>
Test to see‹/›

The following output will be displayed by the above code (this example will not work if the next page does not exist in the history list):

to go to a specific page

You can also usehistory.go()The method loads a specific page from the session history.

This method takes an integer as a parameter.

Negative integers move backward in the history, and positive integers move backward in the history.

<button onclick="history.go("-2);">Back2page</button>
Test to see‹/›

The following output will be displayed by the above code:

Note:If you try to access a page that does not exist in the Windows history, then this methodhistory.back(),history.forward()andhistory.go()No action will be taken.