Using the History Object

Using the History object we can navigate through the current user’s history list. It’s important to note that this object doesn’t provide the exact text of each stop the user has made on the World Wide Web, so we can’t loop through each location and snoop out where the user has been before they hit our page (well… at least not directly). We can, however, use History’s methods to go backward and forward any number of links.

The three methods of History are forward, back, and go. Forward and back are, not surprisingly, the opposite of each other. These methods simply take a positive integer and move forward or back in the history list that specified number of locations. The location is relative, in contrast to the go method, where the location is absolute. So, to implement a function that returned the user to the last page they visited, we’d just need to add the following code to a routine and provide a way for the user to execute this code (we’ll show an example of this in the next chapter):

History.back 1

The go method is also interesting, although it’s hamstrung by problems in the current 3.01 implementation of Internet Explorer. Theoretically (according to the documentation) the go method should take an integer and move to that absolute position in the history list. Calling go with the number 1 should cause the browser to display the first page in the history list. Internet Explorer 3.01 doesn’t implement this behavior exactly, but if you have the time or inclination you might like to play around with this method to see exactly how it behaves in your browser.