Here is a quick snippet that I’ve found useful on several occasions, particularly when you want to build out navigation dynamically and need a quick and dirty way to highlight the currently active page’s link.

Below is the full code, just drop it on your page inside of a tag pair and you’re good to go. If you’d like to know what it does and why, read on.

[code] jQuery(document).ready(function($) { jQuery("*").find("a[href='"+window.location.href+"']").each(function(){ jQuery(this).addClass("active"); //add additional code here if needed }) }); [/code] 

It’s brief, but let’s walk through what it’s doing on the page as it never hurts to know what’s going on if you want to modify, or to simply be aware of what’s running on your site. First, we wrap the whole thing in .ready like so:

[code] jQuery(document).ready(function($) { ... }); [/code] 

This causes the script to fire when the page has reported back to the browser that all elements are loaded, this is important for two reasons, 1) to prevent this script from interacting before we have all our links in place, and 2) to prevent this script from interrupting the page during it’s initial load. You can use the alias “$” instead of “jQuery” here, but I prefer to be explicit in my code so that we can avoid issues later on (and it’s just a handy reminder that hey, we’re using jQuery here). Next, we’re going to use the .find() function to search for all ‚Äò‘ tags on the page, and return to our script an array of links that point to the current page by using a pseudo selector with a bit of JS to fetch the current URL. The full function call looks like:

[code] ... jQuery("*").find("a[href='"+window.location.href+"']") ... [/code] 

Last step, we iterate though the matching links with .each() to apply the class ‘active’ to each.

[code] ... .each(function(){ jQuery(this).addClass("active"); //add additional code here if needed }) ... [/code] 

There you have it, class=—active— will now be applied to any link on the page that currently points to the current url. Keep in mind that you should try to achieve this through the CMS or server side script whenever possible to reduce the JS running in the client browser and minimize page size, but the effects on both here are minimal so this is a perfect way to deal with links outside of navigation, or if you’re unable to achieve this with PHP (et al.) on your site.

If you’re a developer come join us at an upcoming event to geek out with our team on all things code. Events might include lunch & learns on topics such as ecommerce trends, networking events on our roof decks, or meetups of local professionals.

Paul Weinert
Author

Paul Weinert is our founder, managing principal and spirit animal. He’s one of those odd people you meet that was born in Portland, grew up here and still lives here for some reason. If you had two words to describe Paul after meeting him, we’d bet you’d use “charmingly quirky”.

Paul started GRAYBOX in February 2009, and for the first four years, GRAYBOX had a blend of a contract staffing firm and a traditional agency. GRAYBOX then hired our first employees in 2012 and has been growing ever since. Professionally, Paul has a background in internet business consulting, working especially with mid-market ecommerce, healthcare, and tech companies. He worked as a user experience designer, web developer and internet marketer. Nowadays, Paul serves as our company visionary, team mentor and head of executive leadership — Paul is extremely proud of the entire team and the company. He’s constantly humbled by the team’s awesomeness and the caring community that’s sprung up from his initial ideas.

Beyond GRAYBOX, Paul is married, has two young boys and lives in NE Portland. He’s also an active foster parent, mentors other local entrepreneurs, and volunteers on local non-profit boards. Businesswise, aside from GRAYBOX, Paul has started a few other businesses and is an active investor in the local startup scene.

For the sheer joy of it, let’s play 2 truths and a lie:

  1. Paul firmly believes that any activity would be better if on a boat.
  2. Other than this whole Internet thing, Paul has basically no other skills or expertise in anything.
  3. Paul is rare unicorn, actually born and raised in Portland.