If current page url

My code to add a class based on the current page is not working. Can you help me work out why?

 $(document).ready(function() {

$(function(){

    var url = window.location.pathname, 
        urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
        // now grab every link from the navigation

		$('.sub_nav a').each(function(){
            // and test its normalized href against the url pathname regexp
            if(urlRegExp.test(this.href.replace(/\/$/,''))){
                $(this).addClass('sub_nav_active');
            }
        });

});
	
	   });

Open in new window


See this url: http://new2.jensonfisher.com/about-us/values/

Look at the sub navigation below the image.
petewinterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
For one your code structure is a bit odd

$(document).ready(function() {
$(function(){
...

Open in new window

These are effectively the same - use one or the other - not both
$(function() {
...
});

Open in new window


This does not make sense - why are you using RegEx on a static string - when an '==' will do the same thing?
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$")

Open in new window


Here is a stab at doing what you want
$(function() {
  // Get the pathname
  var url = window.location.pathname;

  // If the last char is a '/' then strip it 
  url = (url.substr(-1) === '/') ? url.slice(0, url.length-1) : url;

  // Match the <a> with an href attr that matches the url
  $('a[href="' + url +'"]').addClass('sub_nav_active');
});

Open in new window

0
Julian HansenCommented:
The sample code below demonstrates the concept - working version here
HTML
      <div class="sub_nav">
        <ul>
           <li><a href="/aboutus/values">Values</a></li>
           <li><a href="/aboutus/diversity">Diversity</a></li>                 
        </ul>
      </div> 

Open in new window

JavaScript
$(function() {
  $('.tester').click(function(e) {
    e.preventDefault();
    url = $('#url').val();
	url = (url.substr(-1) === '/') ? url.slice(0, url.length-1) : url;
    $('.sub_nav_active').removeClass('sub_nav_active');
    $('.sub_nav a[href="' + url +'"]').addClass('sub_nav_active');
  });
});

Open in new window

0
petewinterAuthor Commented:
Thanks, but it doesn't seem to work for me. What have I done wrong on this page...

http://new2.jensonfisher.com/about-us/values/

this is the script in the page: http://new2.jensonfisher.com/wp-content/themes/jensonfisher/js/jf-scripts.js
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Julian HansenCommented:
about-us <> aboutus

Your subnav code - no hypen in aboutus
<ul>
  <li><a href="/aboutus/values">Values</a></li>
  <li><a href="/aboutus/diversity">Diversity</a></li>	        			 
</ul>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petewinterAuthor Commented:
Such a silly mistake! Thanks for your help.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.