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
back() method:
<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: • Determine Minimum and Maximum • Use Escape to Replace Dangerous Characters • The Java Script window.open Method • Java Script Trigonometric Methods • Java Script confirm Message Box • The switch / case Structure • JavaScript to Concatenate Strings • JavaScript Operators • Java Script Math.sin Method • Java Script prompt Message Box
|