I know libraries exist for this, but I still wanted to write something of my own, using jQuery and http://benalman.com/projects/jquery-hashchange-plugin/
"nav.js"
I basically have it take a #page/opt=1/optex=2 format and put that into page.php?opt=1&optex=2 and so on.Code:function hashToLink(hash) { //Converts the hash into a regular page, including GET attributes args = ""; hash = hash.substring(1).split("/"); base = hash[0]; for(i = 1; i < hash.length; i++) { args = args + (i == 1 ? "?" : "&") + hash[i]; } return base +".php"+ args; } function loadPage(page) { $(".main-content").fadeTo(400, 0, function () { $.get("pages/"+ page, function(data) { $(".main-content").html(data); }).fail(function() { $(".main-content").html("Could not load 'pages/"+ page +"'."); }); $(".main-content").fadeTo(400, 1); }); } $(document).ready(function() { $(window).hashchange(function() { loadPage(hashToLink(location.hash)); }); loadPage(window.location.hash != "" ? hashToLink(window.location.hash) : "home.php"); //Load selected/default page });
Any criticisms/advice? Thanks



Reply With Quote

