I want to implement a navigation history using jQuery and AJAX in a cross-browser manner. My approach is to use window.history.pushState
and fall back to a hash url /#!/url
in browsers that do not support window.history.pushState
.
For example:
<a href="/home">home</a>
<a href="/about">about</a>
<a href="/contact">contact</a>
On browsers that support window.history.pushState
, clicking on one of these links should change address without page refresh to http://domain.com/home, http://domain.com/about etc. When the browser does not support window.history.pushState
, it should use a fragment identifier, i.e: http://domain.com/#!/home, http://domain.com/#!/about.
Update: Based on the feedback here I have implemented Ajax SEO (git) that uses
jQuery Address for HTML5 History API with old browser fallback to /#!/url
.
It has been a while since my original answer and I would now suggest using Backbone.
An implementation could be:
Excerpt from the documentation:
And now every time
Backbone.history.navigate
is called, theAppRouter
will perform an AJAX load of the path into the#content
div.To handle all links with AJAX you could use the following:
Take note of the
{trigger: true}
which causes the handler in the router to be called (otherwise only from url changes).Fiddle with example code: http://jsfiddle.net/koenpunt/pkAz7/1/
Something i've been using with fallback hash URL's:
You could bind your click events to this with:
If you need some more explanation on this example i'll be glad to hear it.EDIT
Please see my other answer.