I have a sidenav that “reloads” every time, so this will not work:
The solution must be based on url (I think). I have found this code that I think should do the trick.
<div id=”nav”>
<a href=”/about”><button class=”btn active”>About</a>
<a href=”/contact”><button class=”btn”>Contact</button></a>
<a href=”/forum”><button class=”btn”>Forum</button></a>
</div>
<script>
(function() {
alert(window.location.pathname.split(‘/’)[1]) (the slash is removed)
var nav = document.getElementById(‘nav’),
anchor = nav.getElementsByclass(‘btn’),
current = window.location.pathname.split(‘/’)[1];
for (var i = 0; i < anchor.length; i++) {
if(anchor[i].href == current) {
anchor[i].className = “active”;
}
}
})();
</script>
But there is 2 problem:
1. The “window.location.pathname.split(‘/’)[1]” gives “about” but I need “/about”.2. I want to load this javascript when opening the url. What is the syntax for this?.
How do I do this? Thank you for any advice or input on this.