I have a page with some navigation and I'm trying to highlight the link if that's the page we're on. It works almost quite well just like this:
function highlightPageInNav() {
// Get file name.
var filename = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
// Build selector.
var attrSelector = "a[href='" + filename + "']";
// Set class of li to "current".
$("#nav ul li").has(attrSelector).addClass("current");
}
The problem is that the anchors in my navigation say, for example, "CreateUser.aspx", but the script won't match that if the current URL contains "createuser.aspx".
I've tried several other ideas, including using the following to replace part of the script above. I convert the filename toLowerCase() and then try to match it to the href .toLowerCase() and those alerts show me what I expect, but the $(this).addClass("current");
doesn't actually do anything -- most likely, I guess, because $(this)
no longer applies to the element I'm trying to modify.
$("#nav ul li a").each(function () {
alert($(this).attr("href"));
hrefLowerCase = $(this).attr("href").toLowerCase();
alert(hrefLowerCase);
if (filename == hrefLowerCase) {
alert("found one!");
$(this).addClass("current"); // This is where it stops doing what I desire.
}
});
So, if someone can show me how to very simply make my script match both "CreateUser.aspx" and "createuser.aspx", that would be much appreciated! Thanks!