I am using a jQuery script I wrote to load content on my site without refreshing the page, and everything works fine except the back/forward buttons. I am using hash links and loading in my php files based on that.
....
<li><a id="theShopLink" class="theMainLinks" href="#shopFrame">SHOP</a>
....
$('.theMainLinks').click(function() {
var dest = $(this).prop("hash").substring(1);
$('.theMainLinks').removeClass('active');
$(this).addClass('active');
if ($('#fullFrame > div').is(':visible')) {
$('#homeFrame').addClass('animated fadeOutLeft');
$('#homeFrame').html('');
}
console.log(dest);
$("<style>html { background: #000; }</style>").appendTo("head");
$('#nextFrame').html('<div id="loading"><img id="loading-image" src="/wp-content/uploads/assets/loader.gif" alt="Loading..." /></div>').load('/frames/' + dest + '.php');
});
I tried searching and found some examples using html5 window.history.pushState(); but I'm not sure how to work that with my current code without having to rewrite the script. Also the # in the urls look ugly, anyway around this?
I'm not looking to use any kind of plugins/dependencies besides jQuery, and if possible the cleanest/simplest code. Please explain so I can understand because I will need to do something similar with my sub navigation links within the frames and will reuse the function.
UPDATE
On initial website load or refresh, I use the following function to determine what content to load. -
$(function() {
if (window.location.hash) {
var target = window.location.hash.substring(1);
var hash = window.location.hash;
if (hash.toLowerCase().indexOf("frame") >= 0) {
$('#homeFrame').html('');
$('#homeFrame').html('<div id="loading"><img id="loading-image" src="/wp-content/uploads/assets/loader.gif" alt="Loading..." /></div>').load('/frames/' + target + '.php');
} else if (hash.toLowerCase().indexOf("shop-") >= 0) {
$('#homeFrame').html('');
$('#homeFrame').html('<div id="loading"><img id="loading-image" src="/wp-content/uploads/assets/loader.gif" alt="Loading..." /></div>').load('/frames/shopFrame.php');
} else if (hash.toLowerCase().indexOf("news-") >= 0) {
......etc......
} else if (hash.toLowerCase().indexOf("/") >= 0) {
var newTar = target.split('/');
$('#homeFrame').html('');
$('#homeFrame').html('<div id="loading"><img id="loading-image" src="/wp-content/uploads/assets/loader.gif" alt="Loading..." /></div>').load('/general/' + newTar + ' #patchnotesMain');
} else {
// Fragment doesn't exist
$('#homeFrame').html('<div id="loading"><img id="loading-image" src="/wp-content/uploads/assets/loader.gif" alt="Loading..." /></div>').load('/' + target + ' #newContent');
}
} else {
$('#homeFrame').html('<div id="loading"><img id="loading-image" src="/wp-content/uploads/assets/loader.gif" alt="Loading..." /></div>').load('/frames/homeFrame.php');
}
});