Skip to content

Instantly share code, notes, and snippets.

@sahinsf
Created August 28, 2012 19:38
Show Gist options
  • Save sahinsf/3503199 to your computer and use it in GitHub Desktop.
Save sahinsf/3503199 to your computer and use it in GitHub Desktop.
//Top Navigation, adding .active class to a tab upon navigation
//Top Navigation, adding .active class to a tab upon navigation
$(function(){
//deactivate default click action in # <a> links in navbar
$('.nav [href^=#]').click(function (e) {
e.preventDefault()
});
var url = window.location.pathname,
// 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
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
// now grab every link from the navigation
$('.nav a').each(function(){
// and test its normalized href against the url pathname regexp
if(urlRegExp.test(this.href.replace(/\/$/,''))){
//if($(this).parents().hasClass('mainnav')) {
//$(this).parents().filter('.mainnav').addClass('active');
$(this).parents('.mainnav').addClass('active');
//}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment