Chapter 20
Using the History List
What is a History List?
As you surf the Web, you will load many different pages, each with its own URL. The
browser maintains a list of the most recent URLs, which can be viewed with ease in both
Navigator and IE, as demonstrated in the following figures:
Figure
20-1. IE’s history list.
Figure
20-2. Navigator’s history list.
The history list behaves like a LIFO (Last In First Out) queue, where the Back button
climbs up the list so URLs loaded by the Back button are not entered into the history
list. Therefore, the history list does not always contain all the recently visited pages.
For example, if you reach a page named “a.html” and you press the Back button to load
a page named “b.html,” its URL replaces the URL of “a.html.”
The history Object
The history list is represented in JavaScript by the window.history object.
This object lets you deal with the history list but not with its exact data. That is,
actual URLs maintained in that list cannot be extracted or otherwise modified by a script.
The only property of this object is its length. Its methods enable you to load the
list’s entries but not to manipulate the URL explicitly.
You can take advantage of this object to automatically navigate the user’s browser
backwards, as if possessed by spirits. Another possible application is to create the
equivalent of the browser’s Back button directly in the document.
Since the history object is a property of the topmost window object,
you have the option to refer to it as window.history or simply history.
History List Length
You can access the number of entries in the history list via the history.length
property. It works exactly like the strings’ and arrays’ length property. You
can use this property to find how many pages the user has visited lately:
// display message according to number of entries
if (history.length > 10)
alert("You've already accessed " + history.length + " Web
pages this session")
else
alert("You've only accessed " + history.length + " Web pages
this session")
This script displays an alert message which depends on the number of entries in the
history list.
History List Entry Indexing
Like in arrays, each entry of the history list has an index, which differentiates it
from the other elements of the list. However, the indexing method is quite different from
character indexing in strings, or element indexing in arrays. As opposed to these indexing
algorithms, the history list indexing scheme does not feature a minimum value. The index
of the document currently loaded into the browser’s window is 0. The index of the
document that was loaded before the current document, the one that can be reached by
pressing the Back button, is –1. The document before that is indexed at –2, and so on.
Similarly, documents that were first loaded after the current document are indexed
positively. The index of the first document loaded after the current one, the one that can
be retrieved via the Forward button, is 1. The following one is indexed at 2, and so on.
The complete index resembles an axis with no limit at both ends.
The history list is dynamic (changes rapidly) because whenever the page in the
browser’s window is replaced by a new document, the current entry becomes the previous
one, and a new document takes its place. The desired shifting in terms of indexing is
performed automatically by the browser, so you don’t have to worry about it.
Since most people tend to surf different places at different times, the content of the
history list almost never repeats itself. You might think that by creating a very
structured site, you can control the way the user surfs your site and thus be able to
forecast the content of the history list. This is generally impossible, and you should not
even try to do it.