Visit Mozilla.org

DOM:window.history

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

Returns a reference to the History object, which provides an interface for manipulating the browser session history (pages visited in the tab or frame that the current page is loaded in).

[edit] Syntax

historyObj = window.history;

The obtained History object has the following methods:

Method Description Return value
history.back() Goes to the previous page in session history, the same action as when the user clicks the browser's Back button.

Equivalent to history.go(-1).
Calling this method to go back beyond the first page in the session history has no effect and doesn't raise an exception.

No return value
history.forward() Goes to the next page in session history, the same action as when the user clicks the browser's Forward button.

Equivalent to history.go(1).
Calling this method to go back beyond the last page in the session history has no effect and doesn't raise an exception.

No return value
history.go(integerDelta) Loads a page from the session history, identified by its relative location to the current page, for example -1 for the previous page or 1 for the next page.

When integerDelta is out of bounds (e.g. -1 when there are no previously visited pages in the session history), the method doesn't do anything and doesn't raise an exception.
Calling go() without parameters or with a non-integer argument has no effect (unlike Internet Explorer, which supports string URLs as the argument [1]).

No return value

The History object also has the following properties:

Property Type Description
length Integer Read-only. Returns the number of elements in the session history, including the currently loaded page.

For example, for a page loaded in a new tab this property will return 1.

current String Returns the URL of the active item of the session history.

This property is not available to web content and is not supported by other browsers. Use location.href instead.

next String Returns the URL of the next item in the session history

This property is not available to web content and is not supported by other browsers.

previous String Returns the URL of the previous item in the session history

This property is not available to web content and is not supported by other browsers.

[edit] Example

history.back();     // equivalent to clicking back button

[edit] Notes

For top-level pages you can see the list of pages in the session history, accessible via the History object, in the browser's dropdowns next to the back and forward buttons.

For security reasons the History object doesn't allow the non-privileged code to access the URLs of other pages in the session history, but it does allow it to navigate the session history.

There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.

[edit] See also

MSDN: history Object (window)

[edit] Specification

DOM Level 0. Not part of any standard.