3

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

Kikadass
  • 314
  • 1
  • 3
  • 14
  • Just work to give yourself some reputation. You can do this TODAY right now, simply fill out your profile in detail. :) Find another StackOverflow site that interests you, sign up there: find one here: http://stackexchange.com/sites Then answer some questions (pick some simple ones that you feel you can answer; or hard ones that you know something about). Do those things and you will have more than enough reputation to post comments. – Mark Schultheiss Mar 08 '16 at 17:55
  • Just to be clear: do you wish to navigate to a NEW page (those are pages not "sites" by the way) or do you wish to replace the content portion with the new page content? – Mark Schultheiss Mar 08 '16 at 18:04
  • Sorry I'm not english xD I would like to go to a NEW page :) – Kikadass Mar 08 '16 at 18:09

1 Answers1

1

This should give you what you want:

$(function() {
   var mynavbar = $("#navbar"); //cache it
   mynavbar.load("Navbar.html");
   // remove any current active if found in markup:
   mynavbar.find('ul.nav li').removeClass('active');

   var url = window.location;// get location

   // add the active class to current url detected href
   // Will work for absolute hrefs, might have to adjust for others
   mynavbar.find('ul.nav li').find('a').filter(function() {
      return (url.href.indexOf(this.href) != -1);
   }).parent().addClass('active');

  // put the click handler for the navigation in place
  mynavbar.on('click', 'ul.nav li', function() {
     var myhref = $(this).find('a').attr('href');
     // just for debug   alert("proceed to: " + myhref);
     window.location.href = myhref;
  });
});

NOTE: IF you want to simply use the current page, loading content in a hashed area, that is different. In that case you will need to rework this and change this line:

window.location.href = myhref;

For example:

$('#mycontentcontainer').load(myhref);

IF you want to use tabbed content and load the content only on the click, that can also be done (but you said you wanted to navigate to the NEW page so I did not include that)

EDIT: NOTE: Looking ahead, the solution above with load might not work in all cases (load slow)...so use this instead: Only process the `nav after it is loaded.

function addActiveNavbar(mynavbar) {
  // remove any current active if found in markup:
  mynavbar.find('ul.nav li').removeClass('active');
  var url = window.location; // get location
  // Will work for absolute hrefs, might have to adjust for others
  mynavbar.find('ul.nav li').find('a').filter(function() {
    return (url.href.indexOf(this.href) != -1);
  }).parent().addClass('active');
}
$(function() {
  var mynavbar = $("#navbar"); //cache it
  addActiveNavbar(mynavbar);
  $.get("Navbar.html")
    .done(function(data) {
      mynavbar.html(data);
      // add the active class to current url detected href
      addActiveNavbar(mynavbar);
    });
  // put the click handler for the navigation in place
  mynavbar.on('click', 'ul.nav li', function() {
    var myhref = $(this).find('a').attr('href');
    // un-comment for debug   alert("proceed to: " + myhref);
    window.location.href = myhref;
  });
});
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • Thank you. Sorry I'm a noob, so: where do I put this function? in the navbar.html as a script inside the head? and if so does it work automatically or I have to call it somehow? – Kikadass Mar 08 '16 at 20:11
  • Put this somewhere in a .js file and put a link to that in the document AFTER your jQuery link. Put that link on each page that it is needed on. Because this is in the document ready `$(function() {` wrapper, it runs when it is loaded and the document ready event fires and you do not have to specifically call it - the event handler for the document ready fires that. `$(function() {` is the same as `$(document).ready(function() {` that you see places. or if it makes more sense: `$(document).on('ready', function() {` That is simply a wrapper for that. – Mark Schultheiss Mar 08 '16 at 20:16
  • THANK YOU VERY MUUUUUCHHHHH!!!! one last thing is that when I change page a window is opened saying that the page is gonna change. is it good for users(should I keep it) or can I take it away? How can I thank you? is there any way of giving you points of reputation or smthing? – Kikadass Mar 08 '16 at 20:26
  • NO I only added that to assist you (that alert) You should ALWAYS understand any code from anywhere before you add it to your code base/site production code. Including code from StackOverflow or by me or anyone. No need for thanks, accepting answers and up voting stuff you like is what this site is about - I use it to learn stuff as well, sometimes by figuring out the answer and then posting it. – Mark Schultheiss Mar 08 '16 at 20:28
  • Note that I really do NOT do this for rep/score and anyone who does is here for the wrong reason. I only USE that rep to get better answers and use the privileges it empowers. – Mark Schultheiss Mar 08 '16 at 20:33
  • I just realized that it adds the class active before redirecting into the new website. But once is redirected the class active is not there anymore. I have tried to modify the code, but I'm not sure how. I just looked at the console and it is saying that "url.indexOf" it is not a function. Do you know how to fix that? – Kikadass Mar 11 '16 at 12:01
  • 1
    I fixed the href issue, it was using the window.location not the window.location.href and it needed a string (href) not the location object. https://developer.mozilla.org/en-US/docs/Web/API/Location I also adjusted to put the highlight on any "current" nav bar but that gets replaced with the `.get` and likely does nothing useful. I did this by moving the code to a function which is better anyway and then calling it on document ready...NOTE: Use the second code set. – Mark Schultheiss Mar 11 '16 at 14:33
  • 1
    I setup a simulation of the get using a deferred here to test the latest: https://jsfiddle.net/MarkSchultheiss/301oy70u/1/ – Mark Schultheiss Mar 11 '16 at 15:02