I have implemented a function that continuously checks if the URL contains any of the navigation hrefs every 100ms. If it finds a match, specific elements are assigned the class active.
Here's the code snippet:
var checkActive = function(){
var path = window.location.pathname,
path = decodeURI(path),
path = path.replace(/[\/]/, "");
if ( window.location.pathname.indexOf(path) > -1 ) {
$('.navigation .nav li a[href*="' + path + '"]').parent().addClass('active');
$('.navigation .nav li a:not([href*="'+path+'"])').parent().removeClass('active');
}
(function loopingFunction() {
checkActive();
setTimeout(loopingFunction, 100);
})();
However, I am concerned about whether checking every 100ms will impact the performance for users with slower PCs?
P.s: The reason I opted for this approach is because I am utilizing pushState, statechange and .load() to update my content dynamically without refreshing the entire page.