Truncate url value as javascript variable
I am using jQuery Treeview to navigate a site with nested category levels. Each category contains products, each of which has one or more types of information.
So my urls could be:
- foo.com/view/cat_id/
- foo.com/view/cat_id/prod_id/
- foo.com/view/cat_id/prod_id/info_id/
The problem is that the navigation for the products is categorized, which means the Treeview only "knows about" (and therefore marks the "active") case 1. Cases 2 and 3 mean the tree has collapsed, whereas I want the tree that should be open for cat_id for all cases.
(I'm open to anything, but) I think the solution is Javascript trickery to remove anything after cat_id / from the url, so that then pass the Treeview result, but not be like that, Javascript I can't figure out, how to do it.
Note. I'm looking for help with Javascript regex, not Treeview, although in case anyone knows of a ready-made solution, I'm of course all ears.
TIA.
source to share
After the help of a friend (thanks to Sawks), I present the answer to my question in case anyone else ends up in the same boat.
I changed lines starting at 206 in 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;
}
in
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;
}
source to share