Skip to content

Instantly share code, notes, and snippets.

@galaakk
Created March 3, 2013 14:12
Show Gist options
  • Select an option

  • Save galaakk/5076263 to your computer and use it in GitHub Desktop.

Select an option

Save galaakk/5076263 to your computer and use it in GitHub Desktop.
JS: Add active class for current URL (works with all URIs)
var url = window.location;
$('nav a').filter(function() {
return this.href == url;
}).addClass('active');
@galaakk
Copy link
Copy Markdown
Author

galaakk commented Mar 3, 2013

Best way I've found to achieve this effect. All URL formats are ok with this one, it seems.

Just have a nav tag with an ul and some li>a in it, style the .active class in CSS, and let's go !

You need first to call jQuery or Zepto, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment