title: History API - Dive Into HTML5
url: http://diveintohtml5.info/history.html
hash_url: 897f370c439ad4b431206ec71e5dc4fa

<p class="f">The browser location bar is perhaps the geekiest mainstream piece of user interface in the world. There are <abbr>URL</abbr>s on billboards, on the sides of trains, and even in street graffiti. Combined with the back button — easily the most important button in the browser — you have a powerful way to go forward and backward through the vast set of intertwingled resources called the Web.

</p><p>The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html"><abbr>HTML5</abbr> history <abbr>API</abbr></a> is a standardized way to manipulate the browser history via script. Part of this <abbr>API</abbr> — navigating the history — has been available in previous versions of <abbr>HTML</abbr>. The new parts in <abbr>HTML5</abbr> include a way to add entries to the browser history, to visibly change the <abbr>URL</abbr> in the browser location bar (without triggering a page refresh), and an event that fires when those entries are removed from the stack by the user pressing the browser’s back button. This means that the <abbr>URL</abbr> in the browser location bar can continue to do its job as a unique identifier for the current resource, even in script-heavy applications that don’t ever perform a full page refresh.

</p><h2 id="why">The Why</h2>

<p class="ss"><img src="http://diveintohtml5.info/i/openclipart.org_johnny_automatic_demon_reading_Stewart_Orr.png" alt="demon reading book" width="218" height="231"/>

</p><p>Why would you manually manipulate the browser location? After all, a simple link can navigate to a new <abbr>URL</abbr>; that’s the way the web has worked for 20 years. And it will continue to work that way. This <abbr>API</abbr> doesn’t try to subvert the web. Just the opposite. In recent years, web developers have found new and exciting ways of subverting the web without any help from emerging standards. The <abbr>HTML5</abbr> history <abbr>API</abbr> is actually designed to ensure that <abbr>URL</abbr>s continue to be useful in script-heavy web applications.

</p><p>Going back to first principles, what does a <abbr>URL</abbr> do? It identifies a unique resource. You can link to it directly; you can bookmark it; search engines can index it; you can copy and paste it and email it to someone else, who can click it and end up seeing the same resource you saw originally. These are all excellent qualities. <abbr>URL</abbr>s matter.

</p><p>So we want unique resources to have unique <abbr>URL</abbr>s. But at the same time, browsers have always had a fundamental limitation: if you change the <abbr>URL</abbr>, even through script, it triggers a roundtrip to the remote web server and a full page refresh. This takes time and resources, and it seems especially wasteful when you are navigating to a page that is substantially similar to the current page. Everything on the new page gets downloaded, even the parts that are exactly the same as the current page. There is no way tell a browser to change the <abbr>URL</abbr> but only download half a page.

</p><p>The <abbr>HTML5</abbr> history <abbr>API</abbr> lets you do this. Instead of triggering a full page refresh, you can use script to, in essence, download half a page. This illusion is tricky to pull off, and it requires some work on your part. Are you watching closely?

</p><p class="ss"><img src="http://diveintohtml5.info/i/openclipart.org_johnny_automatic_card_trick.png" width="287" height="238" alt="magician performing a card trick"/>

</p><p id="illusion">Let’s say you have two pages, page A and page B. The two pages are 90% identical; only 10% of the page content is different. The user navigates to page A, then tries to navigate to page B. But instead of triggering a full page refresh, you interrupt this navigation and do the following steps manually:

</p><ol>
<li><em>Load the 10% of the page</em> from page B that is different from page A (probably using <code>XMLHttpRequest</code>). This will require some server-side changes to your web application. You will need to write code to return just the 10% of page B that is different from page A. This can be a hidden <abbr>URL</abbr> or query parameter that the end user would not normally see.
</li><li><em>Swap in the changed content</em> (using <code>innerHTML</code> or other <abbr>DOM</abbr> methods). You may also need to reset any event handlers on elements within the swapped-in content.
</li><li><em>Update the browser location bar</em> with the <abbr>URL</abbr> of page B, using a particular method from the <abbr>HTML5</abbr> history <abbr>API</abbr> that I’ll show you in a moment.
</li></ol>

<p>At the end of this illusion (if you executed it correctly), the browser ends up with a <abbr>DOM</abbr> that is identical to page B, just as if you had navigated to page B directly. The browser location bar ends up with a <abbr>URL</abbr> that is identical to page B, just as if you had navigated to page B directly. But you never really did navigate to page B, and you never did a full page refresh. That’s the illusion. But because the “compiled” page looks the same as page B and has the same <abbr>URL</abbr> as page B, the user should never notice the difference (nor appreciate all your hard work micromanaging their experience).

</p><p class="a rotatedFloralHeartBullet">

</p><h2 id="how">The How</h2>

<p>The <abbr>HTML5</abbr> history <abbr>API</abbr> is just a handful of methods on the <code>window.history</code> object, plus one event on the <code>window</code> object. You can use these to <a href="http://diveintohtml5.info/detect.html#history">detect support for the history <abbr>API</abbr></a>. Support is currently limited to the very latest versions of a few browsers, putting these techniques squarely in the “progressive enhancement” camp.

</p><table class="bc">
<caption>history.pushState support</caption>
<thead>
<tr><th title="Internet Explorer">IE</th><th title="Mozilla Firefox">Firefox</th><th title="Apple Safari">Safari</th><th title="Google Chrome">Chrome</th><th>Opera</th><th>iPhone</th><th>Android
</th></tr></thead><tbody>
<tr><td>·</td><td>4.0+</td><td>5.0+</td><td>8.0+</td><td>11.50+</td><td>4.2.1+</td><td>·
</td></tr></tbody></table>

<p><a href="http://diveintohtml5.info/examples/history/fer.html">dive into dogs</a> is a straightforward but non-trivial example of using the <abbr>HTML5</abbr> history <abbr>API</abbr>. It demonstrates a common pattern: a long article with an associated inline photo gallery. In a supported browser, navigating the Next and Previous links in the photo gallery will update the photo in place <em>and update the <abbr>URL</abbr> in the browser location bar</em>, without triggering a full page refresh. In unsupported browsers — or, indeed, supported browsers where the user has disabled scripting — the links simply function as regular links, taking you to a new page with a full page refresh.

</p><p id="gallery-markup">Let’s dig into the <a href="http://diveintohtml5.info/examples/history/fer.html">dive into dogs</a> demo and see how it works. This is the relevant markup for a single photo:

</p><p class="legend top"><span class="arrow anticlockwiseTopSemicircleArrow"/> The pledge
</p><pre><code>&lt;aside id="gallery"&gt;
  &lt;p class="photonav"&gt;
    &lt;a id="photonext" href="http://diveintohtml5.info/casey.html"&gt;Next &amp;gt;&lt;/a&gt;
    &lt;a id="photoprev" href="http://diveintohtml5.info/adagio.html"&gt;&amp;lt; Previous&lt;/a&gt;
  &lt;/p&gt;
  &lt;figure id="photo"&gt;
    &lt;img id="photoimg" src="http://diveintohtml5.info/gallery/1972-fer-500.jpg"
            alt="Fer" width="500" height="375"&gt;
    &lt;figcaption&gt;Fer, 1972&lt;/figcaption&gt;
  &lt;/figure&gt;
&lt;/aside&gt;</code></pre>

<p>Nothing unusual there. The photo itself is an <code>&lt;img&gt;</code> inside a <code>&lt;figure&gt;</code>, the links are just regular <code>&lt;a&gt;</code> elements, and the entire thing is wrapped in an <code>&lt;aside&gt;</code>. It’s important that these are just regular links that actually work. All the code that follows is behind a <a href="http://diveintohtml5.info/detect.html#history">detection script</a>. If the user is using an unsupported browser, none of our fancy history <abbr>API</abbr> code will ever be executed. And of course, there’s always some users with scripting disabled altogether.

</p><p>The main driver function gets each of these links and passes it to a function, <code>addClicker()</code>, which does the actual work of setting up the custom <code>click</code> handler.

</p><pre><code>function setupHistoryClicks() {
  addClicker(document.getElementById("photonext"));
  addClicker(document.getElementById("photoprev"));
}</code></pre>

<p>This is the <code>addClicker()</code> function. It takes an <code>&lt;a&gt;</code> element and adds a <code>click</code> handler. And within this <code>click</code> handler is where it gets interesting.

</p><pre><code>function addClicker(link) {
  link.addEventListener("click", function(e) {
    swapPhoto(link.href);
    history.pushState(null, null, link.href);
    e.preventDefault();
  }, false);
}</code></pre>
<p class="legend right"><span class="arrow leftwardsWaveArrow"/> Interesting

</p><p>The <code>swapPhoto()</code> function performs the first two steps of our <a href="http://diveintohtml5.info/#illusion">three-step illusion</a>. The first half of the <code>swapPhoto()</code> function takes part of the <abbr>URL</abbr> of the navigation link itself — <code>casey.html</code>, <code>adagio.html</code>, <i>&amp;</i>c. — and constructs a <abbr>URL</abbr> to a hidden page that contains nothing but the markup required by the next photo.

</p><pre><code>function swapPhoto(href) {
  var req = new XMLHttpRequest();
  req.open("GET",
           "http://diveintohtml5.info/examples/history/gallery/" +
             href.split("/").pop(),
           false);
  req.send(null);</code></pre>

<p>Here is a sample of the markup returned by <code><a href="http://diveintohtml5.info/examples/history/gallery/casey.html">http://diveintohtml5.info/examples/history/gallery/casey.html</a></code>. (You can verify this in your browser by visiting that <abbr>URL</abbr> directly.)

</p><pre><code>&lt;p class="photonav"&gt;
  &lt;a id="photonext" href="http://diveintohtml5.info/brandy.html"&gt;Next &amp;gt;&lt;/a&gt;
  &lt;a id="photoprev" href="http://diveintohtml5.info/fer.html"&gt;&amp;lt; Previous&lt;/a&gt;
&lt;/p&gt;
&lt;figure id="photo"&gt;
  &lt;img id="photoimg" src="http://diveintohtml5.info/gallery/1984-casey-500.jpg"
          alt="Casey" width="500" height="375"&gt;
  &lt;figcaption&gt;Casey, 1984&lt;/figcaption&gt;
&lt;/figure&gt;</code></pre>

<p>Does that look familiar? It should. It’s the <a href="http://diveintohtml5.info/#gallery-markup">same basic markup that the original page used</a> to display the first photo.

</p><p>The second half of the <code>swapPhoto()</code> function performs the second step of our <a href="http://diveintohtml5.info/#illusion">three-step illusion</a>: inserting this newly downloaded markup into the current page. Remember that there is an <code>&lt;aside&gt;</code> wrapping the entire figure, photo, and caption. So inserting the new photo markup is a one-liner, setting the <code>innerHTML</code> property of the <code>&lt;aside&gt;</code> to the <code>responseText</code> property returned from <code>XMLHttpRequest</code>.

</p><pre><code>  if (req.status == 200) {
    document.getElementById("gallery").innerHTML = req.responseText;
    setupHistoryClicks();
    return true;
  }
  return false;
}</code></pre>

<p>(Also notice the call to <code>setupHistoryClicks()</code>. This is necessary to reset the custom <code>click</code> event handlers on the newly inserted navigation links. Setting <code>innerHTML</code> wipes out any trace of the old links and their event handlers.)

</p><p>Now, let’s go back to the <code>addClicker()</code> function. After successfully swapping out the photo, there’s one more step in our <a href="http://diveintohtml5.info/#illusion">three-step illusion</a>: setting the <abbr>URL</abbr> in the browser location bar without refreshing the page.

</p><p class="legend top"><span class="arrow anticlockwiseTopSemicircleArrow"/> The turn
</p><pre><code>history.pushState(null, null, link.href);</code></pre>

<p>The <code>history.pushState()</code> function takes three parameters:

</p><ol>
<li><code>state</code> can be any <abbr>JSON</abbr> data structure. It is passed back to the <code>popstate</code> event hander, which you’ll learn about in just a moment. We don’t need to track any state in this demo, so I’ve left it as <code>null</code>.
</li><li><code>title</code> can be any string. This parameter is currently unused by major browsers. If you want to set the page title, you should store it in the <code>state</code> argument and set it manually in your <code>popstate</code> callback.
</li><li><code>url</code> can be, well, any <abbr>URL</abbr>. This is the <abbr>URL</abbr> you want to appear in the browser’s location bar.
</li></ol>

<p>Calling <code>history.pushState</code> will immediately change the <abbr>URL</abbr> in the browser’s location bar. So is that the end of the illusion? Well, not quite. We still need to talk about what happens when the user presses the all-important back button.

</p><p>Normally when the user navigates to a new page (with a full page refresh), the browser pushes the new <abbr>URL</abbr> onto its history stack and downloads and draws the new page. When the user presses the back button, the browser pops one page off its history stack and redraws the previous page. But what happens now that you’ve short-circuited this navigation to avoid a full page refresh? Well, you’ve faked “moving forward” to a new <abbr>URL</abbr>, so now you also need to fake “moving backward” to the previous <abbr>URL</abbr>. And the key to faking “moving backwards” is the <code>popstate</code> event.

</p><p class="legend top"><span class="arrow anticlockwiseTopSemicircleArrow"/> The prestige
</p><pre><code>window.addEventListener("popstate", function(e) {
    swapPhoto(location.pathname);
});</code></pre>

<p>After you’ve used the <code>history.pushState()</code> function to push a fake <abbr>URL</abbr> onto the browser’s history stack, when the user presses the back button, the browser will fire a <code>popstate</code> event on the <code>window</code> object. This is your chance to complete the illusion once and for all. Because making something disappear isn't enough; you have to bring it back.

</p><p>In this demonstration, “bringing it back” is as simple as swapping in the original photo, which we do by calling the <code>swapPhoto()</code> with the current location. By the time your <code>popstate</code> callback is called, the <abbr>URL</abbr> visible in the browser’s location bar has been changed to the previous <abbr>URL</abbr>. Also, the global <code>location</code> property has already been updated with the previous <abbr>URL</abbr>.

</p><p>To help you visualize this, let’s step through the entire illusion from the beginning to the end:

</p><ul>
<li>User loads <code><a href="http://diveintohtml5.info/examples/history/fer.html">http://diveintohtml5.info/examples/history/fer.html</a></code>, sees story and a photo of Fer.
</li><li>User clicks the link labeled “Next,” an <code>&lt;a&gt;</code> element whose <code>href</code> property is <code><a href="http://diveintohtml5.info/examples/history/casey.html">http://diveintohtml5.info/examples/history/casey.html</a></code>.
</li><li>Instead of navigating <code><a href="http://diveintohtml5.info/examples/history/casey.html">http://diveintohtml5.info/examples/history/casey.html</a></code> with a full page refresh, the custom <code>click</code> handler on the <code>&lt;a&gt;</code> element traps the click and executes its own code.
</li><li>Our custom <code>click</code> handler calls the <code>swapPhoto()</code> function, which creates an <code>XMLHttpRequest</code> object to synchronously download the <abbr>HTML</abbr> snippet located at <code><a href="http://diveintohtml5.info/examples/history/gallery/casey.html">http://diveintohtml5.info/examples/history/<strong>gallery</strong>/casey.html</a></code>.
</li><li>The <code>swapPhoto()</code> function sets the <code>innerHTML</code> property of the photo gallery wrapper (an <code>&lt;aside&gt;</code> element), thereby replacing the captioned photo of Fer with a captioned photo of Casey.
</li><li>Finally, our custom <code>click</code> handler calls the <code>history.pushState()</code> function to manually change the <abbr>URL</abbr> in the browser’s location bar to <code><a href="http://diveintohtml5.info/examples/history/casey.html">http://diveintohtml5.info/examples/history/casey.html</a></code>.
</li><li>User clicks the browser’s back button.
</li><li>The browser notices that a <abbr>URL</abbr> has been manually pushed onto the history stack (by the <code>history.pushState()</code> function). Instead of navigating to the previous <abbr>URL</abbr> and redrawing the entire page, the browser simply updates the location bar to the previous <abbr>URL</abbr> (<code><a href="http://diveintohtml5.info/examples/history/fer.html">http://diveintohtml5.info/examples/history/fer.html</a></code>) and fires a <code>popstate</code> event.
</li><li>Our custom <code>popstate</code> handler calls the <code>swapPhoto()</code> function again, this time with the previous <abbr>URL</abbr> that by now is already visible in the browser’s location bar.
</li><li>Again using <code>XMLHttpRequest</code>, the <code>swapPhoto()</code> function downloads a snippet of <abbr>HTML</abbr> located at <code><a href="http://diveintohtml5.info/examples/history/gallery/fer.html">http://diveintohtml5.info/examples/history/<strong>gallery</strong>/fer.html</a></code> and sets the <code>innerHTML</code> property of the <code>&lt;aside&gt;</code> wrapper element, thereby replacing the captioned photo of Casey with a captioned photo of Fer.
</li></ul>

<p>The illusion is complete. All visible evidence (the content of the page, and the <abbr>URL</abbr> in the location bar) suggests to the user that they have navigated forward one page and backward one page. But no full page refresh ever occurred — it was all a meticulously executed illusion.

</p>
