Anchor-based URL navigation, Take 2

categories: jquery, plugins

I noticed I was getting a lot of visits via Google for my post on anchor-based URL navigation with jQuery, so I decided to write a plugin that would accomplish the same thing.

Call this function on an element that contains "panels" which, in turn, contain anchors. The container element should contain ONLY related panels, as this plugin will show only one first-child element of the container at once.

This plugin will look at the current URL and see whether it contains an anchor. For example:

http://www.mysite.com/index.html#panel1

If it finds an anchor, it will look for the panel that contains the associated anchor tag; it will show this panel using the function defined by the showFn option, and it will hide the panel's siblings using the function defined by the hideFn option.

If the nav option is set, it will use the nav option setting as a selector to locate the page's navigation. It will look for a link with an href matching the anchor; if it finds a matching link, it will run the function defined by the currentNavFn option on the link element (useful for setting the current nav item).

If the nav option is set, it will also set up onclick functions on each of the links in the nav that refer to anchors on the page; the onclick function will use the show and hide options to show and hide the associated panels.

Options

Default option values:

 
var options = {
  showFn: function() { $(this).show(); },
  hideFn: function() { $(this).hide(); },
  nav: null,
  currentNavFn:
    function() {
      // this will add a class to the parent of the
      // link that matches the currently selected anchor
      $(this).parent().siblings().removeClass('anchor-nav-current');
      $(this).parent().addClass('anchor-nav-current');
    },
  anchorClass: null,
  noAnchorFn:
    function() {
      // this will show the first panel
      // if the URL doesn't contain an anchor
      $container.children().hide().eq(0).show();
    }
};
 

3 Responses to “Anchor-based URL navigation, Take 2”

  1. rdmey | Anchor-based URL navigation with jQuery says:
    December 24th, 2007 at 12:45 pm

    [...] Anchor-based URL navigation, Take 2 [...]

  2. rdmey | Revisiting rebeccamurphey.com says:
    December 26th, 2007 at 5:59 pm

    [...] Anchor-based URL navigation, Take 2 [...]

  3. Jeff Rothe says:
    April 8th, 2008 at 10:42 am

    Rebecca,

    I haven’t ever used jquery before, and I am trying to figure out how to use your plugin. I am kind of at a loss without pin pointed instructions of, “Place this function here, make sure you link to this script file there”. The example you have stays within one page, but I think it should work when linking between two different pages.

    Would you be able to flesh out your instructions for the most simple of users like myself? I am supposed to link to your plugin file and the jquery library in the source file with the list of links. I also link to the library and your plugin on the end html page?

    Jeff

Comment