Firs of all I wanted to say that I have read a lot about it online and all the solutions found I could not implement them properly, some repaired some problems but created others. Another thing is that I cannot comment on other posts because of low reputation (noob), if I could do that I would not be posting a new question.
I am creating a website with a navbar with different pages for each part of the website: Home.html, AboutUs.html...
I implemented the navbar, but I think is not a good solution to paste the same code of the navbar in each page so I looked for a way to optimizing it.
For now I created another site (Navbar.html) and added it into the other pages and it looks good:
<script>
$(function(){
$("#navbar").load("Navbar.html");
});
</script>
and inside the body:
<div id="navbar"></div>
The problem occurred when trying to automatize the "active" class. For that I tried both data-toggle='tab' and data-toggle='pill' but in both cases the active changed but when clicking the button the webite didn't change. There is the code:
<ul class="nav navbar-nav" >
<li class="menuText"><a href="Home.html" data-toggle="tab" >Home</a></li>
<li class="menuText"><a href="AboutUs.html" data-toggle="tab" >About Us</a></li>
<li class="menuText"><a href="Donate.html" data-toggle="tab" >Donate</a></li>
<li class="menuText"><a href="Volunteer.html" data-toggle="tab" >Volunteer</a></li>
<li class="menuText"><a href="Contact.html" data-toggle="tab" >Contact</a></li>
</ul>
I also tried redirecting it with a .js file:
$('#home').load('Home.html');
$('#aboutus').load('AboutUs.html');
$('#donate').load('Donate.html');
$('#volunteer').load('Volunteer.html');
$('#contact').load('Contact.html');
it worked perfectly and it changed pages when the button was clicked, but I found that if I scrolled down there was the first page I opened constantly there below the changing page. (hard to explain this, hope you understood)
I also tried adding lots of functions found online but I didn't really know if they were working. I think I didn't implemented them correctly or in the wrong place. I'm a noob in html. I don't really know how to call the function :S Example:
$(function(){
var hash = window.location.hash;
hash && $('ul.nav a[href="Home.html"]').tab('show');
$('.nav-tabs a').click(function (e) {
$(this).tab('show');
var scrollmem = $('body').scrollTop();
window.location.hash = this.hash;
$('html,body').scrollTop(scrollmem);
});
});
Is there anyway I can do it, or another way to automatize the "active" class in each page?
Thank you for your time