1. Home
  2. Computing & Technology
  3. JavaScript

The Browser Object Model

history

The window.history object within the Browser Object Model contains the history of the web pages that the browser has visited. This does not mean that the actual information in the history list is available from JavaScript. To protect the privacy of the person using the browser there is only limited ways in which JavaScript can interact with this object.

The only property of the history that JavaScript has access to read is history.length which tells you how many entries that there are in the history. Obtaining that information is almost useless since there is no way of telling which of those entries represents the current page and so you don't really know whether any preceding or following entries in the history exist. Provided that the person hasn't gone back in order to get to the current page from one that they subsequently loaded the current page will be at the end of the list with no entries following but you have no way to tell if they have gone back and if so how far they have gone back.

The only methods available for interacting with the history list are ones which will load a different page from the list. The problem here is that all the methods only allow you to specify which entry from the list to load relative to the current entry and you have no idea before making the call whether that entry even exists. The three methods for moving to a different page from the list are:

  • history.back() to load the immediately preceding page from the list (if there is one)
  • history.forward() to load the immediately following page from the list (if there is one)
  • history.go(9) to load the page a specified number of entries away from the current page (assuming that the entry exists)

In the case of history.go() negative numbers in the parameter passed to it indicate the number of entries to go back in the history while positive numbers go forward that many entries.

The history.back() and history.forward() methods (along with history.go(-1) and history.go(1) which do exactly the same) simply duplicate the browser back and forward buttons which are much more easily accessed from the keyboard simply by holding down the alt key and pressing the left or right arrow keys or by opening the contextmenu and selecting back or forward from there. As such these methods serve little purpose in a web page.

While calls such as history.go(-2) do not have equivalent functionality built directly into the browser most browsers provide a simple way to actually display the content of the history list (which JavaScipt can't do) making it easier to select the exact page that the person wants to go back or forward to.

Overall then you are much better off educating your visitors in how to use their browser (assuming that they are new to the web and haven''t already learnt how it works) than you are in trying to use the limited access that JavaScript provides to the history list.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.