Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 750067 of PopStateEvent

  • Revision slug: Web/API/PopStateEvent
  • Revision title: PopStateEvent
  • Revision id: 750067
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

Revision Content

 

{{APIRef("HTML DOM")}}

An event handler for the popstate event on the window.

A popstate event is dispatched to the window every time the active history entry changes between two history entries for the same document. 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 clicking on the back button (or calling history.back() in JavaScript). And the event is only triggered when the user navigates between two history entries for the same document.

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.

Syntax

window.onpopstate = funcRef;
  • funcRef is a handler function.

The popstate event

As an example, a page at https://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: https://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: https://example.com/example.html, state: null
history.go(2);  // alerts "location: https://example.com/example.html?page=3, state: {"page":3}

Note that even though the original history entry (for https://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().

Specification

See also

Revision Source

<div>&nbsp;</div>

<p id="Summary" name="Summary">{{APIRef("HTML DOM")}}</p>

<p>An event handler for the <code>popstate</code> event on the window.</p>

<p>A <code>popstate</code> event is dispatched to the window every time the active history entry changes between two history entries for the same document. If the history entry being activated was created by a call to <code>history.pushState()</code> or was affected by a call to <code>history.replaceState()</code>, the <code>popstate</code> event's <code>state</code> property contains a copy of the history entry's state object.</p>

<p>Note that just calling <code>history.pushState()</code> or <code>history.replaceState()</code> won't trigger a <code>popstate</code> event. The <code>popstate</code> event is only triggered by doing a browser action such as a clicking on the back button (or calling <code>history.back()</code> in JavaScript). And the event is only triggered when the user navigates between two history entries for the same document.</p>

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

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
window.onpopstate = <var>funcRef</var>;
</pre>

<ul>
 <li><var>funcRef</var> is a handler function.</li>
</ul>

<h2 id="The_popstate_event" name="The_popstate_event">The popstate event</h2>

<p>As an example, a page at <code><var>https://example.com/example.html</var></code> running the following code will generate alerts as indicated:</p>

<pre class="brush:js">
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: https://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: https://example.com/example.html, state: null
history.go(2);  // alerts "location: https://example.com/example.html?page=3, state: {"page":3}
</pre>

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

<h2 id="Specification" name="Specification">Specification</h2>

<ul>
 <li><a href="https://www.whatwg.org/specs/web-apps/current-work/#handler-window-onpopstate">HTML5 popstate event</a></li>
</ul>

<h2 id="See_also" name="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history">Manipulating the browser history</a></li>
 <li><a href="/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history/Example">Ajax navigation example</a></li>
</ul>
Revert to this revision