Let’s try playing with HTML5 history starting with something simple:
Nothing incredible so far. Now let’s have the links to load the content in the div:
Now the requests are made with AJAX and the browser history is not updated, thus the browser back button will not work as expected. Let’s first update the browser address bar:
Note the history.pushState(stateObj, title, url) method. It has 3 parameters:
stateObj is a JSON string we can save along the history item
title is the page title
url is the url that will be shown on the address bar
The back button doesn’t work yet. Let’s take care of that:
I’ve not been able to fetch back the data parameter of the pushState method. I would have liked to store the requested URL there. Any clue?
Shorter version
If we’re using Rails RJS files there’s a simple way to use all this: