mozilla
Your Search Results

    popstate Redirect 2

    The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.

    Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by doing a browser action such as a click on the back button (or calling history.back() in JavaScript).

    Browsers tend to handle the popstate event differently on page load. Chrome and Safari always emit a popstate event on page load, but Firefox doesn't.

    General info

    Specification
    HTML5
    Interface
    PopStateEvent
    Bubbles
    Yes
    Cancelable
    No
    Target
    defaultView
    Default Action
    None

    Properties

    Property Type Description
    target Read only EventTarget The browsing context (<code>window</code>).
    type Read only DOMString The type of event.
    bubbles Read only boolean Does the event normally bubble?
    cancelable Read only boolean Is it possible to cancel the event?
    state Read only any The current history entry's state object (if any).

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support Yes 4.0 (2) 10.0 Yes limited
    Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support 3.0 (buggy in 2.2 and 2.3) 4.0 (2) 10.0 Yes limited

    Example

    A page at http://example.com/example.html running the following code will generate alerts as indicated:

    window.onpopstate = function(event) {
      alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
    };
    history.pushState({page: 1}, "title 1", "?page=1");
    history.pushState({page: 2}, "title 2", "?page=2");
    history.replaceState({page: 3}, "title 3", "?page=3");
    history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
    history.back(); // alerts "location: http://example.com/example.html, state: null
    history.go(2);  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}
    

    Note that even though the original history entry (for http://example.com/example.html) has no state object associated with it, a popstate event is still fired when we activate that entry after the second call to history.back().

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,