Anchor-based URL navigation, Take 2
Update: I encourage everyone who arrives at this post to check out Ben Alman's jQuery BBQ plugin -- it provides a ton of functionality above and beyond what this plugin offers, is much more robust, and is built to take advantage of the latest version of jQuery. While you're welcome to use the plugin on this page, it is no longer supported.
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
- showFn function to show the current panel
- hideFn function to hide the current panel's siblings
- nav selector for the page's navigation section
- currentNavFn function to run on the link in the nav that is associated with the anchor
- anchorClass class assigned to anchor tags; setting this will improve the speed on pages with lots of links
- noAnchorFn function to run if the URL does not contain an anchor
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(); } };

December 24th, 2007 at 12:45 pm
[...] Anchor-based URL navigation, Take 2 [...]
December 26th, 2007 at 5:59 pm
[...] Anchor-based URL navigation, Take 2 [...]
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
December 17th, 2008 at 4:04 pm
Hi Rebecca,
How to move all the anchor tags to the top of the page?
I don’t want to jump to respective anchor.
You know?
July 21st, 2009 at 3:16 pm
I, like Jeff, have been trying to figure out how to use your plugin, but without the step by step instructions, I am at a loss. I’ve been working on this for a few hours and have been unable to get anywhere
October 26th, 2009 at 7:13 am
For those having trouble with this plugin, I’m just about to implement it in the next few hours on a client project, so I’ll let you know how I get on it with it.