demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Self-Aware Site Navigation With jQuery

Recently I demonstrated a simple “animated tabs” site navigation bar in CSS3. While the styles work well, there was an issue with the tab that represented the current, active page. In the code I provided in the article, it was necessary to apply a particular style for the current page tab to the appropriate link in the markup. The markup was:

  1. <ul role="navigation">
  2. <li><a href="#">Home</a></li>
  3. <li><a href="#">About Us</a></li>
  4. <li><a href="#">Products</a></li>
  5. <li><a href="#">Contact</a></li>
  6. <li><a href="#">Your Privacy</a></li>
  7. </ul>

While the CSS for the id was:

  1. ul[role=navigation] li a#forefront {
  2. position: relative; top: -0.2rem; z-index: 2;
  3. }

This is fine in principle, but the approach means that when we make a new page, we cannot simply copy and paste the navigation bar code; we must alter the application of the id to the tab that represents the new page, which also means that we cannot use the navigation as part of a server-side include.

In a previous article I have discussed a way of getting around this issue by using PHP. The advantage of a server-side method is that it always works, but at the cost of a small hit to server performance. If you could guarantee that the majority of visitors had JavaScript enabled on their browsers, you could use a client-side solution instead; here, I’ll use jQuery to keep the code concise.

After eliminating any reference to the id in our navigation bar markup (since what we are about to do will be generated solely by JQuery) you would need to add the following into the <head> of each page; ultimately, of course, this could be made part of an include also:

  1. <script src="http://code.jquery.com/jquery.min.js"></script>
  2. <script>
  3. $(document).ready(function(){
  4. $('ul[role="navigation"] a').each(function() {
  5. if (this.href === window.location.href)
  6. { $(this). attr('id', 'forefront');}
  7. });
  8. })
  9. </script>

This script is compact and efficient: it only loops through links found in an unordered list with the ARIA role of navigation on the page (note that selecting this in JQuery is exactly the same as the equivalent CSS attribute selector). Obviously the example at the top of this article is simply there for show, as this blog lacks a products page.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.