I was able to put together a simplified complete History.js example using three links to load fragments of content from a full page without updating the page and while updating the browser history.
Here are the relevent code snippets - a complete working example is here http://jsfiddle.net/PT7qx/show
<a href="/page-header">Page Header</a>
<a href="/login-form">Login Form</a>
<a href="/nothing">Nothing</a>
<script>
var History = window.History;
if (!History.enabled) {
return false;
}
History.Adapter.bind(window, 'statechange', function() {
var State = History.getState();
History.log(State.data, State.title, State.url);
if (State.title == 'Page Header') {
$('#content').load('/user/login/ .pageHeader > *');
}
if (State.title == 'Login Form') {
$('#content').load('/user/login/ #common-form > *');
}
if (State.title == 'Nothing') {
$('#content').empty()
}
});
$('body').on('click', 'a', function(e) {
var urlPath = $(this).attr('href');
var Title = $(this).text();
History.pushState(null, Title, urlPath);
// prevents default click action of <a ...>
return false;
});
<\script>
I would like to know if this is the correct usage. The previous version had an ability to bind to an event using the # urls. I have not seen any examples for binding events to urls with this latest version so I used the .on() click event to call History and sorted out what link was clicked in there.
I am not sure if this is the best way to accomplish this for this example.
History.pushState({urlPath: window.location.pathname()}, $("title").text(), State.urlPath);
will throw an error, as window.location.pathname() isn't a function. Just remove the parentheses and it works like a charm. – Latrishalatry