8cb49c7ceb3bf250a0d470009ac9f047

I'm using this plugin in combination with 'superfish' style css navigation menu to add a 'selected' class to the anchor tag for the page the user is currently on.

Basically this plugin loops through the LI elements, searching for an anchor tag with a href attribute that matches the pages current URL. It will also drill down into nestled LI's - and add the class to the parent anchor tag. So if you are on a dropdown navigation item, the parent item will be selected.

It's called by:
$("ul.sf-menu > li").SelectedNav();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(function($) {
    $.fn.SelectedNav = function(options) {

        options = $.extend({
            position: 0 // this sets the position of the 'home' nav option. If no matches are found, we highlight the anchor at this index.
        });

        var curUrl = window.location.href;


        this.each(function() {

            var cur = $(this), // current  LI
                i = $("a", cur), // anchor in LI
                href = i.attr("href"); // href of anchor

            if (href != "/") { //if the href isn't for the home page

                $("ul > li > a", cur).each(function() { // loop to check for child nodes for
                    if (CheckURL($(this))) { //if we have a match...
                        AddClass(i); //add the class
                        return false; // exit the each statement
                    }

                });
                if (CheckURL(i)) { //if we still haven't got a match check the normal non child nodes
                    AddClass(i);
                    return false;
                }

            }
        });

        if ($("ul > li > a.selected", this).size() == 0) { // if we STILL don't have a match, add it to the position as specified by the options (eg home page)
            $(this).eq(options.position).first().addClass("selected");
        }

        function CheckURL(a) {
            if (curUrl.indexOf(a.attr("href")) > 0) {
                return true;
            }

            return false;
        }

        function AddClass(a) {
            a.first().addClass("selected");
            
        }
    }
})(jQuery);

Refactorings

No refactoring yet !

8cb49c7ceb3bf250a0d470009ac9f047

chad.tolkien.id.au

February 16, 2010, February 16, 2010 03:22, permalink

No rating. Login to rate!

I've changed this to use the standard jquery selector for top level navigation - then if we don't find a match in the top level navigation, we foreach cascadingly looking for a hit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(function($) {
    $.fn.SelectedNav = function(options) {

        options = $.extend({
            position: 0,  // this sets the position of the 'home' nav option. If no matches are found, we highlight the anchor at this index.
            className: "selected", // this sets the default name of the class to use
            homeHref: "/" // this sets the href for the home/default/index page
        });

        var curUrl = window.location.href;
        curUrl = curUrl.substring(curUrl.lastIndexOf('/'));

        var navEle = $("> li > a[href$=" + curUrl + "]", this); //lets look at the top level anchors for a hit
        if (navEle.size() > 0) {
            navEle.addClass(options.className);
        }
        else { //we didn't find a hit, we have to loop  through
            $("li", this).each(function() {
                var cur = $(this), // current  LI
                    i = $("a", cur), // anchor in LI
                    href = i.attr("href"); // href of anchor
                
                $("ul > li > a", cur).each(function() { // now loop through the child nodes
                    if (CheckURL($(this).attr("href"))) { //if we have a match...
                        AddClass(i); //add the class to the parent anchor
                        return false; // exit the each statement
                    }
                });
            });

        }
        
        function CheckURL(a) {
            if (curUrl == a) {
                return true;
            }
            return false;
        }

        function AddClass(a) {
            a.first().addClass(options.className);
        }
    }

})(jQuery);
55c142d920e2bb2d0ebfb16d30c73991

Anurag

March 13, 2010, March 13, 2010 21:14, permalink

No rating. Login to rate!

I think you can avoid manually stepping down the li hierarchy, and do a single search. Once you find the matching anchor, just keep stepping up through parent anchors until you find one that is also a top level anchor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var currentUrl = window.location.href;

// store all top level links
var topLevelLinks = $("> li > a", this);

// find exact anchor matching page url
var currentLinkSelector = "li > a[href=​​​​​​​​​​​​​​​​​​​​​​​{url}]".replace("{url}", curUrl);​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
var currentLink = $(currentLinkSelector, this);

// keep stepping up to parent anchors until we find a match in our top level links
var link = currentLink;
while(!$.inArray(topLevelLinks, link)) {
    link = link.closest('a');
}
​
558cd775e2af0294d243e78c83506a98

xXmikeGalXx

July 20, 2010, July 20, 2010 22:20, permalink

No rating. Login to rate!

I'm trying to open forum but sometimes there are no images on it :(

62a40ff1b5f93544112002e7c4220dc0

HuiFungPung

August 6, 2010, August 06, 2010 03:50, permalink

No rating. Login to rate!

Do you know any good new male enhancement product?
Lets discuss it here. I'm interested in all information about new products of this kind.

Your refactoring





Format Copy from initial code

or Cancel