The Browsers History Object
By Stephen Bucaro
The Back and Forward buttons are related to the browser's history object. As the user
surfs the Web, the history object maintains a list of the URLs they visited. When you
select the browser's History menu item you can view the contents of the history object.
The history object is not in the browser's DOM (document object model) because it's
not a visual object, it's a list of past pages that were loaded into the browser.
For security reasons, there is very little you can do with the history object, for
example you can't examine the URL's of the previous websites that the user visited.
The history object's back() method moves the browser back one page in the history
list. Shown below is the code for a Back button using the history object's
<input type="button" value="Back" onclick="history.back()" />
If the user executed the history object's back() method, or clicked the browsers
Back button at least once, then the browser will be at a previously visited URL
in the history list and the history object's forward() method can be used to
move the browser forward one page in the history list.
Shown below is the code for a Back button using the history object's forward() method:
<input type="button" value="Forward" onclick="history.forward()" />
The history object's go() method accepts an integer value that moves the browser
that many pages in the history list from its current location in the list. If the value
passed to the go() method is a negative value, it will move the browser backward
in the history list. If the value passed to the go() method is a positive value,
it will move the browser forward in the history list.
Shown below is the code for a Back button using the history object's go()
method that moves the browser backward 2 pages in the history list:
<input type="button" value="Back" onclick="history.go(-2)" />
Passing the value 0 to the go() method reloads the current page, which may be
something you need to do in code, for example, if a user has not completed a form properly.
More Java Script Code:
• Accessing Web Page Elements
• Search and Replace Strings
• Access Web Page Elements With getElementById
• The while Loop
• Java Script Trigonometric Methods
• Comparison Operators
• Find a Character or a Substring Within a String
• The Location Object
• Interactively Set Webpage Colors