I have an ajax site using jQuery, and I am wondering how to go about allowing the user to refresh the page, and have the appropriate content loaded.
I have it so that when a user clicks on an element with an href attribute with my website domain, that page's content is loaded into the main div. The loaded html content is just an entire html document, so I am not loading page fragments. I am using History.js to keep the URL and hashchanges in check. But when I refresh the page, only the static content is loaded (unstyled), and not with the containing header and footer of the main, home page. The other problem is that I need to clear the existing hash to avoid a GET 404 error. What I obviously would like is for the entire page to load normally.
Here is my code so far:
jQuery(document).ready(function() {
// Prepare
var History = window.History; // Note: We are using a capital H instead of a lower h
if ( !History.enabled ) {
console.log("History is not enabled.");
// History.js is disabled for this browser.
// This is because we can optionally choose to support HTML4 browsers or not.
return false;
}
else {
console.log("History is enabled");
}
//vars for history
var changes = 0;
// Bind to StateChange Event
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
var State = History.getState(); // Note: We are using History.getState() instead of event.state
History.log(State.data, State.title, State.url);
});
$("a").click(function(e) {
e.preventDefault();
$("#content").load(this.href, function() {
// Change our States
changes++;
History.pushState({state:changes}, "State " + changes, "#" + this.href); // logs {state:1}, "State 1", "?state=1"
});
});