- Joined
- Jul 26, 2006
- Messages
- 3,634
- Reaction score
- 1,007
I know libraries exist for this, but I still wanted to write something of my own, using jQuery and
"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.
Any criticisms/advice? Thanks
You must be registered to see links
"nav.js"
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
});
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.
Any criticisms/advice? Thanks