window.history

Table of contents

  1. 1. Summary
  2. 2. Syntax
  3. 3. Example
  4. 4. Notes
  5. 5. See also
  6. 6. Specification
Table of contents
  1. 1. Summary
  2. 2. Syntax
  3. 3. Example
  4. 4. Notes
  5. 5. See also
  6. 6. Specification

« Gecko DOM Reference

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).

Syntax

var 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).

Note: 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; this is equivalent to history.go(1).

Note: 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). No return value
history.pushState(data, title [, url]) Requires Gecko 2.0

Pushes the given data onto the session history stack with the specified title and, if provided, URL. The data is treated as opaque by the DOM; you may specify any JavaScript object that can be serialized.  Note that Firefox currently ignores the title parameter; for more information, see manipulating the browser history.

Note: In Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) through Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2) , the passed object is serialized using JSON. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3) , the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
No return value
history.replaceState(data, title [, url]) Requires Gecko 2.0

Updates the most recent entry on the history stack to have the specified data, title, and, if provided, URL. The data is treated as opaque by the DOM; you may specify any JavaScript object that can be serialized.  Note that Firefox currently ignores the title parameter; for more information, see manipulating the browser history.

Note: In Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) through Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2) , the passed object is serialized using JSON. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3) , the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.
No return value

See Manipulating the browser history for examples and details. In particular, that article explains security features of the pushState() and replaceState() methods that you should be aware of before using them.

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 returns 1.
current Non-standard 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 Non-standard 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 Non-standard 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.
state Requires Gecko 2.0 nsIVariant

Returns the state at the top of the history stack. This is a way to look at the state without having to wait for a popstate event. Read only.

Example

history.back();     // equivalent to clicking back button
history.go(-1);     // equivalent to history.back();

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.

Tags (4)

Edit tags

Attachments (0)

 

Attach file