Avatar of Pete Winter
Pete Winter
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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.
jQueryJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Pete Winter

ASKER
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
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Pete Winter

ASKER
Such a silly mistake! Thanks for your help.
Julian Hansen

You are welcome.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes