Skip to content

Instantly share code, notes, and snippets.

@mundry
Last active December 31, 2015 04:39
Show Gist options
  • Save mundry/7935532 to your computer and use it in GitHub Desktop.
Save mundry/7935532 to your computer and use it in GitHub Desktop.
Highlighting of the current page in the navigation with JavaScript for very simple static pages.
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style> .current { color: #FFAA44; } </style>
</head>
<body>
<div class="navigation">
<ul>
<li><a href="/index.html">Home</a></li>
<li><a href="/products.html">Products</a></li>
<li><a href="/references.html">References</a></li>
<li><a href="/partners.html">Partners</a></li>
<li><a href="/about.html">About Us</a></li>
</ul>
</div>
<script>
(function() {
var navigationLinks = document.querySelectorAll('.navigation li a');
if (window.location.pathname === "/") { // Home
navigationLinks[0].className = 'current';
} else { // Subpages
for (var i = 0; i < navigationLinks.length; i++) {
if (navigationLinks[i].href.search(window.location.pathname) > -1) {
navigationLinks[i].className = 'current';
break;
}
}
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment