Anchor-based URL navigation with jQuery

categories: front-end development, javascript, jquery

Update: I've since written a anchor-based URL navigation plugin that attempts to capture what's described below.

--

I'm working on an application that has several panels of content, each accessed by clicking on a link in the page's main navigation. I wanted to give users the ability to get back to a panel after they'd left the application, without having to click on the panel in the navigation again, so I set it up so the URL would include an anchor link indicating the current panel (e.g. http://www.mysite.com/index.html#panel1).

This had the added benefit of encouraging me to be a good front-end developer by following the principles of progressive enhancement -- before I set this up, a user without Javascript wouldn't get anywhere when they clicked on a link in the page's main navigation, and that's no good. Now, users without Javascript will be taken down the page to the panel they want, and users with Javascript will get the fancy Web 2.0 panels that show and hide and fade and stuff.

Here's what I did:

The end. Now users who arrive via a URL that includes an anchor tag will go to the proper panel, and everyone else will get the default behavior of going to the first panel.

7 Responses to “Anchor-based URL navigation with jQuery”

  1. rdmey | Anchor-based URL navigation, Take 2 says:
    December 24th, 2007 at 12:40 pm

    [...] 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 [...]

  2. Quiller says:
    February 21st, 2008 at 11:53 pm

    Good tip! jQuery is great for little tricks like this. I would, however, suggest using trigger() to simulate an event. It’s more cross-browser friendly and ensures the binding is called. Some time, somewhere, this will save you a few hours of troubleshooting!

  3. Ariel Flesler says:
    February 27th, 2008 at 10:54 pm

    Hi Rebecca
    Have you checked jQuery.LocalScroll ?
    http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html

    Cheers

  4. links for 2008-04-22 « Mike Does Tech says:
    April 21st, 2008 at 7:43 pm

    [...] rdmey | Anchor-based URL navigation with jQuery (tags: jquery programming ajax navigation) [...]

  5. 摘錄若干 jQuery 連結 says:
    August 31st, 2008 at 9:46 am

    [...] Anchor-Based URL navigation with jQuery 使用錨點 1:觀念講解 [...]

  6. The Rollsteady Network » Blog Archive » links for 2008-11-11 says:
    November 11th, 2008 at 7:30 pm

    [...] Anchor-based URL navigation with jQuery | rdmey (tags: javascript jquery web) « MmmMm doughnuts [...]

  7. James says:
    January 31st, 2009 at 11:16 pm

    nice tip! got the idea and solve my problem :)

    thanks

Comment