I commented on jsfiddle which was getting there, but plnkr.co is the best option for this example.
My working example: https://plnkr.co/edit/9YHEVfPqdqafBjiKbueS?p=preview
HTML
<!DOCTYPE html> <html> <head> <title>Home</title> <script data-require=" jquery@1.11.3 " data-semver="1.11.3" src="https://code.jquery.com/jquery-1.11.3.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" data-semver="3.3.6" data-require=" bootstrap-css@3.3.6 " /> <script src="code.js"></script> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a href="#" class="navbar-brand"> Tool</a> </div> <div> <ul id="navbar" class="nav navbar-nav"> <li> <a href="index.html">Home</a> </li> <li> <a href="options.html">Options</a> </li> <li> <a href="ratings.html">Ratings</a> </li> <li> <a href="admin.html">Admin</a> </li> </ul> </div> </div> </nav> <div class="col-sm-3 col-md-2 sidebar"> <ul class="nav nav-sidebar"> <li class="active"> <a href="#">Add </a> </li> <li> <a href="#">View/Modify</a> </li> <li> <a href="#">Delete</a> </li> </ul> </div> </body> </html>
JQuery
$(function() { var url = location.href.toLowerCase(); var action = getURLParam("action"); console.log("Current URL: ", url); console.log("Action: ", action); function getURLParam(sParam) { var sPageURL = window.location.search.substring(1); var sURLVariables = sPageURL.split('&'); for (var i = 0; i < sURLVariables.length; i++) { var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam) { return sParameterName[1]; } } } // Highlight Active link for current page $("#navbar li a").each(function(k, v) { var target = v.href.toLowerCase(); console.log("Seeking withinin: ", target); if (url.indexOf(target) !== -1) { console.log(url.indexOf(target), " Active: ", this); $(this).parent().addClass("active"); } if(url.lastIndexOf("/") + 1 == url.length){ console.log("No match, highlighting Home."); $("#navbar li a[href='index.html']").parent().addClass("active"); } }); // Populate Action links and highlight $(".nav-sidebar li a").each(function(k, v) { var targetPage = url.substring(url.lastIndexOf("/") + 1); if(targetPage.indexOf("?")){ targetPage = targetPage.substring(0, targetPage.indexOf("?")); } if ($(this).text().toLowerCase() == action) { console.log("Highlighting Action: ", $(this).text().toLowerCase()); $(this).parent().addClass("active"); } v.href = targetPage + "?action=" + $(this).text().toLowerCase(); }); });
In this example, you will need to change the path back to your own. When you click "Home", "Option", "Ratings" or "Admin", they will be highlighted (the parent gets the class active ) when the page loads. When you click Add, Show / Change, Delete, it gets highlighted (class li.
Based on your description, this applies to the various scenarios described.
source share