SHARE
Facebook X Pinterest WhatsApp

Working with the History API in HTML5

Written By
thumbnail
Vipul Patel
Vipul Patel
Apr 12, 2014

HTML5 provides a standardized way to interact with browser history in a scripted fashion. Our browsing patterns and today’s rich media content means that our browsers are working more than ever before to fetch the content and display to the user. URLs are increasingly becoming critical pieces of information, be they part of a user’s bookmarks or as a link one shares on their Linkedin/Facebook profile.

The ability to add entries to the browser history and change the URL displayed in the browser bar is something new introduced as part of the history API in the HTML5 specification.

Historically, every new URL in the browser location bar represents a full page refresh. This can be pretty resource intensive if the web page contains rich media. HTMl5 seeks to eliminate that by the history API.

The HTML5 specification says…

Before we dig deeper on the problems that the history API solves, let us take a quick look at the History interface mentioned in the HTML5 specification at http://www.w3.org/html/wg/drafts/html/master/browsers.html#history .

interface History {
 readonly attribute long length;
 readonly attribute any state;
 void go(optional long delta);
 void back();
 void forward();
 void pushState(any data, DOMString title, optional DOMString? url = null);
 void replaceState(any data, DOMString title, optional DOMString? url = null);
};

 

We can see that there are a few APIs and a few properties. We will dig deeper into them later on.

 

How History API helps

When we have pages which are nearly similar in content, every refresh presents wasted effort (bandwidth/processing/etc.). The history API enables smarter way to download only the portion of the page which is different from the current page (by using scripts)

By fetching only the differentiated content from the new location, swapping the changed content current displayed in the browser and updating the browser location bar with the new URL, we can potentially save a lot of network calls and also provide performance boost to the user experience, while preserving the user browse history.

 

Support for history API by a particular browser can be checked by looking at both window.history property and history.pushState. If a browser reports positive on both counts, then it supports the history API.

 

To add entry to the browser history and the location in the browser address bar, the history.pushState API can be called.

window.history.pushState(objectStateData, “string”, “URL”);

 

The above APIs takes in three arguments – first, an object which can contain state data, second, the title of the web page, and third, the URL to be shown in the browser address bar.

 

Hands On

 

For the purpose of the demo, we are loosely basing our sample snippet to the example provided in the HTML5 specification.

 

<!DOCTYPE html>

<title>Number Game – 10</title>

<p>You are at coordinate <span id=”coord”>0</span></p>

<p>

<a href=”?x=11″ onclick=”go(1); return false;”>Forward to 11</a> or

<a href=”?x=9″ onclick=”go(-1); return false;”>Back to 9</a>?

</p>

<script>

var currentNumber = 0; // prefilled by server

function go(d) {

setupPage(currentNumber + d);

history.pushState(currentNumber, document.title, ‘?x=’ + currentNumber);

}

onpopstate = function (event) {

setupPage(event.state);

}

function setupPage(page) {

currentNumber = page;

document.title = ‘Current number – ‘ + currentNumber;

document.getElementById(‘coord’).textContent = currentNumber;

document.links[0].href = ‘?x=’ + (currentNumber + 1);

document.links[0].textContent = ‘Forward to ‘ + (currentNumber + 1);

document.links[1].href = ‘?x=’ + (currentNumber – 1);

document.links[1].textContent = ‘back to ‘ + (currentNumber – 1);

}

</script>

In the above code snippet, on every click, we are executing the function “go” which, in additional to updating the page to reflect the number progress, also uses the history.pushState API to add activities to the browser history as well as update the location in the address bar.

If you watch the activities in a web debugger, you will notice that no new HTML is being fetched; however, the browser seems to think it has visited a bunch of web pages. That is the history API in action.

Summary

In this article, we learned basics behind the history API. I hope you have found this information useful.

 

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com

References

http://www.w3.org/html/wg/drafts/html/master/browsers.html#history

 

Recommended for you...

Best VR Game Development Platforms
Enrique Corrales
Jul 21, 2022
Best Online Courses to Learn HTML
Ronnie Payne
Jul 7, 2022
Working with HTML Images
Octavia Anghel
Jun 30, 2022
Web 3.0 and the Future Of Web Development
Rob Gravelle
Jun 23, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.