Reputation: 357
So, I am writing a script that is suppose to illuminate different sections of the nav. each nav link is set up to be /directory/subdir/. It worked until I realized there needs to be more to this. if you are in a directory that is not the index file it doesn't work since it was directly matching HREF attribute. Now, how would I strip out the filename.html from the link and just get the directory? Thanks for all your help contributors!
Upvotes: 1
Views: 86
Reputation: 357
/****** LINK DETECTION ******/
var nav = $('.mainNav a');
var loc = document.location.pathname;
var pathname = document.location.pathname.substring(1);
var parts = pathname.split(/\//);
var x = parts.length;
var hrefStr = "/"+parts[0]+"/"+parts[1]+"/";
switch (x) {
case 2:
$('.mainNav a').each(function(index, element) {
if ($(this).attr('href') === loc) {
var node = nav.eq(index).parent('li');
var gpnode = node.parents('li');
node.addClass('active');
gpnode.addClass('active');
}
});
break;
default:
$('.mainNav a').each(function(index, element) {
if ($(this).attr('href') === hrefStr) {
var node = nav.eq(index).parent('li');
var gpnode = node.parents('li');
node.addClass('active');
gpnode.addClass('active');
}
});
}
/****** END LINK DETECTION ******/
Upvotes: 0
Reputation: 10636
If i don't misunderstand, you're trying to hilight the your menu's link with
href="/dir/abc/"
while the page address is something like :
/dir/abc/index.shtml
/dir/abc/detail.shtml
/dir/abc/etc.shtml
right ? I've edited your code a bit. Hope this help :
var loc = document.location.pathname;
var nav = $('.mainNav a');
nav.each(function(index, element) {
var href = $(this).attr('href') //-- edited
if (loc.indexOf(href)==0) { //-- edited
var node = nav.eq(index).parent('li');
var gpnode = node.parents('li');
node.addClass('active');
gpnode.addClass('active');
}
});
Upvotes: 1