views:

36

answers:

3

Hey everyone... thanks for taking the time to read this.

I have a JavaScript (jQuery) navigation bar on my main page that is simply "included" on my page. For example, I have index.shtml that "includes" the nav bar, which is "nav_bar.shtml". And this is the same for every other page. Now clearly, with the current setup, there's no way to show the user which menu item is currently selected since the nav_bar.shtml page stays static.

What I'm wanting to do is STILL have just the one nav_bar.shtml file, but be able to, on the individual pages, show the user the current menu item selected on the nav bar (as in a different shade of color, etc.). If nav_bar.shtml stays static, there's not a very clear way to do this. Is there a workaround to this if I don't want to instantiate an entirely new Javascript nav bar on each and every page? Or would each page need its own version of the nav_bar code specific to that page so it knows which item it needs to shade?

Thanks people.. much appreciated.

Mike

A: 

You could attempt to detect which page the user is currently on by use of window.location.pathname, and shade the relevant menu item based on this.

Example:

function shadeMenuItem(item){
    //Your code to style the given element here
}
$(document).ready(function() {
    if(window.location.pathname.match(/^\/questions.*/)){
        shadeMenuItem($('#questions'));  //shade menu item 'questions'
    }
    else if(window.location.pathname.match(/^\/users.*/)){
        shadeMenuItem($('#users'));  //shade menu item 'users'
    }
});

If this code was implemented on this page, the first condition would be matched, meaning that shadeMenuItem() would have the element with #questions passed to it.

Michael Robinson
A: 

I don't know if this is the answer you're looking for. But you don't need any javascript in order to shade certain element in certain page.

You can always take advantage of CSS selectors, for example:

<body id="homepage">
   <ul id="tabs">
     <li id="tab-homepage"><a href="...">homepage</a></li>
     <li id="tab-news"><a href="...">news</a></li>
     ...

In your CSS you can say something like:

#homepage #tab-homepage { background-color: red }
#newspage #tab-news { background-color: blue }

So, finally, you would only have to change the "id" attribute of the body element to get your shaded menu items.

Anyway, if you're using jQuery you can always use something like:

$('body').attr('id', '...'); 
JuanIgnacioIglesias
I think he wants to keep the nav_bar.shtml file static but shade the menu item for the current page... Either he's going to have to use Javascript, or have a different nav_bar file included on each page.
Michael Robinson
You're right. Maybe he could use window.location and a CSS3 selector to get the menu item that points to that location and then change the attribute of the item.
JuanIgnacioIglesias
A: 

One way to do this is to write some code to look in your menu, find the hrefs of the links therein, and compare them to the current window.location.pathname. If you have nice clean URLs this isn't too hard. If you have long, complex URLs, it may not be workable.

So, here's the general idea:

$(document).ready( function(){
  var thispage = location.pathname.substring(1);
  $('#menu li a[href~="' + thispage + '"]') // ~= is contains. Tweak as needed.
    .addClass('active');
});

Assuming your menu looks something like this:

<ul id="menu">
  <li><a href="page1.html">This</a></li>
  <li><a href="page2.html">That</a></li>
  <li><a href="page3.html">The Other</a></li>
</ul>

And of course:

li.active {
    background-color: yellow;
}
Ken Redler