1. Computing & Technology

JavaScript By Example

Browser Object Model: 2. history

From , former About.com Guide

The history object gives very limited access to the browser history. In fact there are only two things you can do with it - you can find out how many pages there are in the history by checking history.length and you can go to a different page in the history by using history.go(). Since you are more likely to want to go back or forward by one page at a time there is also history.back which does the same as history.go(-1) and history.forward() which does the same as history.go(1).

The following example goes back two pages in the history if there are enough pages in the history for it to be likely that it is possible to do so (that is it assumes that your visitor hasn't already stepped back too far through the history already as while it is possible to know how many entries there are in the history we can't tell which of them is the current page).


if (history.length > 2)
  history.go(-2);

Reader Submissions: Help Others By Providing Your Own JavaScript Examples

JavaScript By Example

©2012 About.com. All rights reserved.

A part of The New York Times Company.