Javascript History


The JavaScript History object is used to record the access history of the operation browser. The History object is part of the window object and can be accessed through the window.history property.

Note: History object is the effective scope of the current window.


The length property of the History object

The History object has a unique length attribute that is used to get the number of URLs in the browser's visit history. Examples are as follows:

document.write(history.length);

Output

0

The output of this example depends on the current page browsing record, if it is a new window to open the example, IE browser will output 0 (from 0 to calculate), and Firefox, Chrome and other browsers will output 1.


History object method

History object has the following three methods:

  • history.back() : return to the previous browser page (if present)
  • history.forward() : Go to next page browser (if exists)
  • history.go() : Go to history list a specified page (if present)

back() method

The back() method is used to return to the previous browser page (if exists), which is equivalent to clicking the browser's return button or calling history.go (-1).

<a href="javascript:window.history.back()" />Previous Page</a>

The forward() method

The forward() method is used to navigate to the next browser page (if exists), which is equivalent to clicking the browser's forward button or calling history.go (1)

<a href="javascript:window.history.forward()" />Next Page</a>

go() method

The go() method is used to go to a specific page in the history list (if exists), with the following syntax:
history.go( number|URL )
// Number : The position of the page to be accessed relative to the current page, 
//           a negative integer indicates a backward return, 
//           and a positive integer indicates forward. 
// URL : The URL to access, or a substring of the URL. 

The following example has the same effect as history.back():

<a href="javascript:window.history.go(-1)" />Previous Page</a>



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook