Friday, March 4, 2011

Shorten URL value as Javascript variable

I'm using the jQuery Treeview plugin for navigation on a site with nested levels of categories. In each category there are then products, each of which have one or more information types.

So my URLs can be either:

  1. foo.com/view/cat_id/
  2. foo.com/view/cat_id/prod_id/
  3. foo.com/view/cat_id/prod_id/info_id/

Trouble is, the navigation for the products is separate to the categories, which means Treeview only 'knows about' (and therefore marks 'active') case 1. Cases 2 & 3 mean that the tree is collapsed, whereas I want the tree to be open to cat_id for all cases.

(I'm open to anything, but) I'm thinking the solution is some Javascript trickery to remove anything after cat_id/ from the URL so that I can then pass the result to Treeview, but not being that au fait with Javascript I can't figure out how to do it.

Note: I'm looking for help with the Javascript regex, not Treeview, though on the off-chance that anyone knows of a ready-rolled solution I am of course all ears.

TIA.

From stackoverflow
  • After help from a friend (thanks Sawks) I am presenting the answer to my own question in case anyone else finds themselves in the same boat.

    I changed the lines starting 206 within jquery.treeview.js from:

    case "location":
        var current = this.find("a").filter(function() { return this.href.toLowerCase() == location.href.toLowerCase(); });
        if ( current.length ) {
         current.addClass("selected").parents("ul, li").add( current.next() ).show();
        }
        break;
    }
    

    to:

    case "location":
        var current_location = location.href.toLowerCase();
        var current_base = current_location.split('/').slice(0,5).join('/')+'/';
        var current = this.find("a").filter(function() { return this.href.toLowerCase() == current_base; });
        if ( current.length ) {
         current.addClass("selected").parents("ul, li").add( current.next() ).show();
        }
        break;
    }
    

0 comments:

Post a Comment