I’ve been reading about the HTML5 history API and so far, I haven’t found a simple working demo that shows the mechanics with code.
Here is a working jsfiddle: 4 buttons and 4 divs. When the user presses a button, it shows the corresponding panel.
What I’m looking to do is:
1) rewrite the URL so that when the user is on panel 4 the url ends with /Panel4
2) make the back button and forward button work with the history API.
I know there’s the history.js plug-in but I want to understand how the API works in its simplest form.
Hopefully, the jsfiddle will help others who’ll come to this page looking for a code demo.
Thanks.
Ok, I created what I think is the SIMPLEST form of history API demo.
It can’t work in jsfiddle because it needs to run in its own window. It will however work if you copy-paste the code in notepad, add a reference to jquery where indicated, and save it on your desktop as an html file. Of course, it doesn’t work in IE, but we all know that.
I’ve put in two versions: the one that works without the URL rewrite component (it’ll work from your desktop) and I’ve also commented out the version where you can manipulate the URL. For the latter, you need to run it from a server, either remote or local.
I’ve struggled to get it working across all browsers because Chrome, Safari and Firefox work differently! Here’s the code:
Upvote if it works for you.
Enjoy!