Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Quick tip: how to make active links in your navigation bar
Old 09-10-2008, 01:23 PM Quick tip: how to make active links in your navigation bar
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
This subject recently came up here.

Many websites, in their main navigation, have an indicator in the navigation of which page you are on, anything from the lightening of the link to give the appearance of a tab, to a separate link color, or boldness, etc. I wanted to show everyone a simple way to do this with JavaScript, that will work even if your pages have subsections of dynamic content on them (ex: http://example.com/news?article=21).

Let's start with an HTML structure. For our purposes, this will be a simple menu contained by an unordered-list:
HTML Code:
<ul id="nav">
     <li><a href="/">Home</a></li>
     <li><a href="/about.html">About</a></li>
     <li><a href="/contact.html">Contact</a></li>
</ul>
Notice that the <ul> has an id of "nav" on it. This is important as we'll be using it to target the anchors within it. Now, let's assume that all of these pages are linked to a JavaScript file called "navigation.js":

Let's link to this file at the very bottom of the page, right before the closing </body> tag. We could also place it inside of an onload event, but that would mean waiting for the images to load, which would not be optimal in this case:
HTML Code:
<script type="text/javascript" src="navigation.js"></script>
</body>
navigation.js:
Code:
var menu = document.getElementById("nav").getElementsByTagName("a");
//creates an array out of each "a" element
for(var i=0; i<menu.length; i++) {
    if(menu[i].href == "http://"+window.location.hostname+window.location.pathname) {
    // comparing each href vs this allows for query and hash variables
        menu[i].className="active";
   // add class of "active" if there is a match
    }
}
Now, we make a style for the class="active" which is being added to the link if its href matches our special window location. In this case, we'll simply make the color of the link red:

CSS
Code:
a.active {
     color: red ;
}
That's it, simple as eating cake. The nice thing about this method, as opposed to other ways of doing this, is that it is easy to add many more menu items to the navigation, without ever needing to add more logic to your script, or needing to manually insert anything else to make the navigation behave. You simply link to the same script.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
 
Register now for full access!
Reply     « Reply to Quick tip: how to make active links in your navigation bar
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.08818 seconds with 12 queries