Monday, August 26, 2013

jQuery add class based on page URL


Jquery

$(function(){ function stripTrailingSlash(str) { if(str.substr(-1) == '/') { return str.substr(0, str.length - 1); } return str; } var url = window.location.pathname; var activePage = stripTrailingSlash(url); $('#left-nav ul li a').each(function(){ var currentPage = stripTrailingSlash($(this).attr('href')); if (activePage == currentPage) { $(this).addClass('active'); } }); });

No comments:

Post a Comment