Team:Freiburg/Templates/html/js.html
From 2014.igem.org
(Difference between revisions)
(2 intermediate revisions not shown) | |||
Line 174: | Line 174: | ||
<script> | <script> | ||
- | |||
// remove url in front of anchor links so scroll spy behaves correctly | // remove url in front of anchor links so scroll spy behaves correctly | ||
var navLinks = $('#content-nav .nav a'); | var navLinks = $('#content-nav .nav a'); | ||
Line 180: | Line 179: | ||
// check all links' href attributes for equality with url | // check all links' href attributes for equality with url | ||
var currentPath = location.pathname; | var currentPath = location.pathname; | ||
- | |||
for (var i=0;i<navLinks.length;i++) { | for (var i=0;i<navLinks.length;i++) { | ||
var href = navLinks[i].getAttribute('href') | var href = navLinks[i].getAttribute('href') | ||
- | |||
var indexOfPath = href.indexOf(currentPath); | var indexOfPath = href.indexOf(currentPath); | ||
- | |||
if (indexOfPath === -1) continue; | if (indexOfPath === -1) continue; | ||
+ | if (indexOfPath > 2) continue; | ||
+ | |||
var indexOfSharp = href.indexOf('#'); | var indexOfSharp = href.indexOf('#'); | ||
- | if (indexOfSharp === -1) { | + | // check if the url part of the href ends with our location |
- | navLinks[i].setAttribute('href', '#'); | + | var hreflength = href.length; |
- | + | if (indexOfSharp !== -1) hreflength = indexOfSharp; | |
- | + | if (indexOfPath >= hreflength - currentPath.length - 1){ | |
- | + | if (indexOfSharp === -1) { | |
+ | navLinks[i].setAttribute('href', '#'); | ||
+ | navLinks[i].setAttribute('class', 'active'); | ||
+ | } else { | ||
+ | navLinks[i].setAttribute('href', href.slice(indexOfSharp)); | ||
+ | } | ||
} | } | ||
} | } |
Latest revision as of 22:56, 17 October 2014