Skip to content

Instantly share code, notes, and snippets.

@Matteo182
Created September 12, 2019 10:40
Show Gist options
  • Save Matteo182/4bdba73dc1af020515e98f827eb98f15 to your computer and use it in GitHub Desktop.
Save Matteo182/4bdba73dc1af020515e98f827eb98f15 to your computer and use it in GitHub Desktop.
Add class Active in navbar menu
$(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
$('#box-menu a').each(function(){
// and test its normalized href against the url pathname regexp
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
//CSS
//.active{
// color:red;
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment