ngusum
ngusum

Reputation: 41

Highlight Current Page in Navigation Bar

I tried to give a shot at the problem posed in the subject line. So below is my HTML and jQ for what I'm trying to accomplish.

html


<ul class="topnav1">
   <li class="highlight">
      <a  href="index.php">Home</a>
   </li>
   <li>
      <a  href="testimonials_page.php">Testimonials</a>
   </li>
   <li>
      <a href="services.php">Services</a>
   </li>
   <li>
      <a href="contact_page.php">Contact  Us</a>
   </li>
</ul>

jQ


<script type='text/javascript'>
//Menu highlights.
$(document).ready(function(){
   var pathname =  (window.location.pathname.match(/[^\/]+$/)[0]);
   $(".topnav1 li a").each(function() {
      if ($(this).attr('href')==pathname) {
         $("li.highlight").removeClass("highlight");
         $(this).parent().parent().addClass("highlight");
      }
   });
   $("li.highlight"').parents().each(function(){
      if ($(this).is("li")){
         $(this).addClass("highlight");
      }
   });
});
</script>

The idea is to remove the highlighted class from its default list item, and assign it to the list item whose href attribute matches the current url. I must admit I'm not the best at programming matching patterns, so I'm kind of at a loss as to how to match only part of the url with the href attribute and I'm not sure that's why my code isn't working( the highlight is retained on the home menu item and isn't applied to the others). Any ideas?

Upvotes: 0

Views: 2091

Answers (3)

elad silver
elad silver

Reputation: 9695

This did the trick for me:

  var domain = '{{ DOMAIN }}'; // www.example.com or dev.example.com
  var domain_index =  window.location.href.indexOf(domain);
  var long_app_name = window.location.href.slice(domain_index+domain.length+1); 
  // this turns http://www.example.com/whatever/whatever to whatever/whatever
  app_name = long_app_name.slice(0, long_app_name.indexOf('/')); 
  //now you are left off with just whatever

then you use jquery to add class active

$('nav a[href*="' + app_name+'"]').closest('li').addClass('active');

and of course the css:

.active{background:red;}

this works if you have your html like this:

<ul><li><a href="ee">ee</a></li><li><a href="dd">dd</a></li></ul>

this will atumatically add class active using the page url and color your background to red if your in www.somesite.com/ee thaen ee is the 'app' and it will be active

Upvotes: 0

David Thomas
David Thomas

Reputation: 253486

I'd suggest the following:

// in real life use: var curURL = document.location.toString();
var curURL = 'http://fiddle.jshell.net/_display/testimonials_page.php';

$('.topnav1 li.highlight').removeClass('highlight');
$('.topnav1 li a').each(
    function(){
        if (curURL.indexOf(this.href) != -1){
            $(this).closest('li').addClass('highlight');
        }
    });

JS Fiddle demo.

References:

Upvotes: 1

Highway of Life
Highway of Life

Reputation: 24431

You could achieve the same result by doing the following:

var filename = window.location.pathname.match(/[^\/]+$/)[0];
$('li.highlight').removeClass('highlight');
$('ul.topnav1 li a[href="' + filename + '"]').parents('li').addClass('highlight');

Since you can use the jQuery attribute selector, you can do a strict check and let jQuery do the work, tag[attribute="value"]

Upvotes: 0

Related Questions