Self-written hashchange navigation

Results 1 to 1 of 1
  1. #1
    The Gamma..? EliteGM is offline
    MemberRank
    Jul 2006 Join Date
    NandolandLocation
    4,078Posts

    Self-written hashchange navigation

    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"
    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




Advertisement