The History Object

The history object contains information about all of the different URLs that a client has visited, stored in the history list of the browser.

Method Description
back Takes an integer and moves back this many places in the history list
forward Takes an integer and moves forward this many places in the history list
go Takes a string that represents all or part of a URL and attempts to move to this URL in the history list (also takes a positive or negative integer)

The forward and back methods work identically. Each method takes a positive integer and moves the specified number of places forward or back in the history list. The go method works similarly if the argument you pass is an integer. In this case a negative integer will move backwards and a positive integer forwards. With IE4 you can also pass the go method a string representing all or part of a URL. If this URL exists in the history list, the browser will display the page.

History's only property, length, shows the number of elements in a collection.

If you attempt to move past either the beginning or end of the history list with any of these methods, the browser won't raise an error. Instead you'll just be left at the page at the beginning or end of the history list

The history object can be used to create a very useful part of most web pages: a back button or link that doesn't require a hard-coded URL. Instead, we can just execute either of the following lines of code whenever the link or button is pressed:

   history.back 1

or:

   history.go –1

Both lines of code use the history object to move to the previous page in the history list.

© 1997 by Wrox Press. All rights reserved.