views:

428

answers:

3

This doesn't seem to work in Jquery

$(document).ready(function() {

    $(".navlist li").each(function() {
        var href = $(this).find("a").attr("href");


        if ($(this).find("a").attr("href") == window.location.pathname) {
           $(this).attr("class", "active"); 
        }
    });

});

in my html

<div id="main-navigation">
                <ul class="navlist">   
                    <li><a href="<%=ResolveUrl("~/home.aspx")%>">home</a></li>                 
                    <li><a href="<%=ResolveUrl("~/sample-templates/view-list.aspx")%>">manage sample templates</a></li>
                    <li><a href="<%=ResolveUrl("~/fractional-templates/manage.aspx")%>">manage fractional templates</a></li>
                    <li><a href="<%=ResolveUrl("~/faq.aspx")%>">faq</a></li>
                </ul>
            </div>  

in my css

.navlist li.active a
{
    color: #fff;
    background-color: #369;
    text-decoration: none;  
}

Any suggestions?

I debugged the js and the problem is that the class is not being added

+1  A: 

Does the first selector work? Have you tried debugging it? Say, add some alert("i am here") before var href=... If yes, are you sure that the comparison $(this).find("a").attr("href") == window.location.pathname is ever true?

Maybe you can try a "smarter" jQuery select, where you first match all A elements that have the required url and then just add the classname:

$(".navlist li a[href='" + window.location.pathname +"']").parent().attr("class", "active");

EDIT: There may be a problem with naming. Since "class" is a reserved word, you have to use "className" attribute! so, my code becomes:

$(".navlist li a[href='" + window.location.pathname +"']").parent().attr("className", "active");

or even better:

$(".navlist li a[href='" + window.location.pathname +"']").parent().addClass("active");
naivists
thanks, yes, i verified that the comparison holds true when it should. the code works on IE7 but not on IE6.
gnomixa
i just tried your code and it again works on IE7 and not IE6.
gnomixa
So you mean that it actually enters the `if(..)` body even in IE6, but never sets the class attribute. What if you substitute the `.attr("class", "active")` with `.addClass("active")`?
naivists
that's exactly it - it enters the if statement, but won't add the class. I tried both addClass and the attr methods, neither works. I googled it and apparently CSS2 is not supported in IE6...I am not really a browser expert, so I am not sure if this is the reason. But my code works perfectly in FF and IE7, just not IE6.
gnomixa
try setting both `class` and `className` attributes. This should fix IE6 (see comments of this article: http://woork.blogspot.com/2008/01/change-class-attribute-using-javascript.html )
naivists
jQuery `attr` changes the name `class` to the proper JavaScript property name `className` for you.
bobince
naivists
document.getElementById("idElement").setAttribute("className", "myClass");document.getElementById("idElement").setAttribute("class", "myClass");don't work either.
gnomixa
try `document.getElementById("idElement").className="myClass"`. Sorry for throwing unvalidated ideas at you, but I don't have IE6 where I could test them :-)
naivists
thanks:) it's ok. I think at the end I just might tell users to upgrade. It's an intranet site. But it will drive me nuts meanwhile.
gnomixa
can't you just do it on the server side? Like, checking if the resolved URL matches the `Page.URL` (don't know what language that is)
naivists
interesting twist - i looked at the dev tool bar and the class does get loaded. so the issue must be with my css
gnomixa
try putting a `<li class="active">` in your HTML code, without any jquery - does it show anything?
naivists
just tried your advice - bypassing the jquery now, working with old js, css and html....background color still won't show up in IE6
gnomixa
A: 

Comparing attr('href') against location.pathname isn't so reliable. attr() reads JavaScript properties, not HTML attributes; the JavaScript property for href is normally a resolved absolute URI. jQuery tries to work around some of the problems with that difference, but I don't know that it's always successful.

What would be more reliable would be remember that an HTMLAnchorElement object also works as a Location object, so you can compare them pathnames directly:

if (this.hostname===location.hostname && this.pathname===location.pathname)

This now checks that it's an internal link with the same path as the current page. It ignores any trailing ?query or #hash parts.

$(this).attr("class", "active"); 

The addClass you mention in the title is definitely a better way of saying that.

This definitely should work in IE6. What won't work in IE6, which might be tripping you up, is that though you can have an element with multiple classes, you can't have a CSS selector with multiple class requirements on the same element, eg.:

a.mylink.active { ... }

this will incorrectly require only the active class in IE6. Many other selectors aren't supported in IE6 either, for example the > child selector:

div.thing > a.action { ... }

will never match in IE6.

bobince
Thanks, pathname comparison works, so that's not a problem. addClass() function doesn't work either ....please read my comments reggarding both above. I am also not using 2 classes either - look @ my code. Any other ideas?
gnomixa
actually, `$this` does refer to a LI element, since it calls each() on a collection of list elements.
naivists
I've just tried the example and it works fine for me in IE6. Whatever the problem is, it's not in the code above. I still suspect something IE6 can't parse in the stylesheet. Can we see a test page that breaks for you?
bobince
A: 

It was css

it should be

.navlist .active a:link, .active a:visited, .active a:visited, .active a:hover{
    color: #fff;
    background-color: #369;
    text-decoration: none;  
}

instead of

.navlist li.active a
{
    color: #fff;
    background-color: #369;
    text-decoration: none;  
}
gnomixa
:-) funny that you spent that much time looking in the wrong direction
naivists
granted, i did post my css here - so people were invited to look and point it out:)
gnomixa