Route32

Simple Hash Location Router, executes callback on location hash change that matches declared routes. Intenteded to be use as a piece or standalone on JavaScript mvc apps.

by SnakeFox

HTML

<ul>
   <li><a class="nav" href="#/blog/ww">Caribean</a></li>
   <li><a class="nav" href="#/posts/ee">Central Valley</a></li>    
</ul>

JavaScript

//BEGIN
//Requires jQuery
if(typeof jQuery != "undefined"){
/* begin Route32 */
    function Route32(options){
        //Settings
        var settings = $.extend({
                           'automatic':true,
                           'selector':'.nav'
                       },options);
        //array of hashes containing hashsregexpstr,callbackfunc pairs  
        var routes = [];
        
        var activeHash = '';
            
        //methods
        var methods = {
            //initial method
            'init':function(){
                window.onhashchange = function(evt){
                       activeHash = "#" + evt.newURL.split("#")[1];           
                        //activeHash = methods.getHashValue();
                    };
            },
            //verifies is string is a valid location hash 
            'isValidHash':function(hashStr){
                                return true;
                            },
            'isValidCallbackfunc':function(callbackfunc){
                                    if(typeof callbackfunc == "function"){
                                        return true;           
                                    }else{
                                        return false;
                                    }
                                  },
            'getHashValue':function(){
                                return window.location.hash;                
                            },
            'executeCurrent':function(){      
                                $.each(routes,function(index,value){           
                                    if(value.hash == activeHash){
                                        value.callback();
                                    }                                    
                                });
                            }            
                      };        
        //adds routes
        this.add = function(hashRegexpStr,callbackfunc){
           ...