Okay so I'm trying to make a good ajax navigation setup that will use pushState / popstate html5 api.
- I'm trying to use no hash fragment
- Intact bookmarks
- Page refresh will bring you to the current state of the ajax nav
Code I got at the moment:
Javascript stuff
Code
$(function() {
$('nav a').click(function(e) {
href = $(this).attr("href");
loadContent(href); //function to call on nav click event
history.pushState("", "", href); //append new href to end of url
e.preventDefault();
});
window.onpopstate = function(event) { //on a url change (back / forward browser button)
loadContent(location.pathname); //load content for that specific url
};
});
function loadContent(url){
$.ajax({
type: "GET",
url: "content_manager.php", //make ajax request to this php file
data: {"content_href" : url}, //send it the href
success: function(data) {
$("#content").html(data); //get returned data put it into the #content div
}
});
$('li').removeClass('current'); //make it pretty
$('a[href="'+url+'"]').parent().addClass('current'); //make it pretty
}
Code
<?php
$content_href = $_GET["content_href"]; //get the content href sent by the ajax call
switch(content_href){ //switch
//different cases to test for (yes I'm aware they do different things this is the bit where it's messing up so I'm experimenting)
case "portfolio": echo("<p>include/portfolio.php</p>"); break;
case "about": echo("<p>include/about.php"</p>); break;
case "resume": $content_href = "include/resume.php"; break;
case "contact": $content_href = "include/contact.php"; break;
default: echo("<p>include/404.php</p>"); //this one works but the others don't..
}
?>
So my question is does it work the same as I think it does?
Why does only the switch default statement work on a page refresh (popstate trigger)?
How would I go about sending a different file in the callback data? e.g I make the ajax request to the contnet_manager.php file rather than having that hold all the html stuff (gets messy really quickly), instead have it load the content from separate files?
Any useful resources / material you could link me to would be cool!
Thanks!