1

Edit: I suspect the problem has to do that since all my # anchors are on the same page, unless I reload the page, JavaScript just doesn't know about my clicks on the link. I don't know how to solve this problem though...:(

I'm trying to make a clicked link stay blue if the section it points to is shown. This is the code (I put the Javascript code just before the closing body tag as suggested by this tutorial).

HTML

<ul id="menu">
    <li><a href="#section1">section1</a></li>
    <li><a href="#section2">section2</a></li>
    <li><a href="#section3">section3</a></li>

JavaScript

links = document.getElementById("menu").getElementsByTagName("a");

for(i=0; i<links.length;i++) {
   if(links[i].href == window.location.hash) {
   links[i].className = "active";
    }
}

CSS

a.active {
    color:blue;
}

However it does not seem to work. Can anyone help? Thanks!

5
  • do you get an javascript error on the page? Commented Apr 19, 2012 at 12:56
  • 1
    Add console.log(links[i].href+" "+window.location.hash) inside your loop but outside the if and see what you get. Commented Apr 19, 2012 at 12:56
  • Please post your real HTML. The markup you did post isn't even close to valid. The <a> tags are left unclosed with > Commented Apr 19, 2012 at 12:58
  • there are no javascript errors; how do I check the console log? Yes, I just corrected my code; thanks for pointing that out! Commented Apr 19, 2012 at 13:20
  • The console log says 127.0.0.1/application/home.html#section1 127.0.0.1/application/home.html#section2 127.0.0.1/application/home.html#section3 Commented Apr 19, 2012 at 13:29

3 Answers 3

3

Your anchor tags aren't closed, make sure they look like this:

<a href="#section1">Link Text</a>

Notice the closing " and > that your code is missing.

Also, did you wrap the JavaScript code in a <script> block?

  <script>
    all of your javascript inside of this script block
  </script>
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks; I'm a really lousy coder:( Yes, the code is wrapped in a <script> block...
2

Your href may be absolute in that case, not just hashes:

   if(links[i].href == window.location.hash)

3 Comments

I changed my code to if(links[i].href == "http://"+window.location.hostname+window.location.pathname); but it doesn't help any either...
why not just: if(links[i].href == window.location.href) ?
Yes, I did just that too; but it's not helping. I noticed that when the page first loads, the console indicates every window.location.href of links[i] is something.com/index.html. When I click on a link, the window.location.href doesn't get changed. However, if I say click on the link that points to #section2 and hit the reload button, window.loacation.href for all links[i] become something.com/index.html#section2, and the section2 link becomes blue too.
1

Maybe you need to put the script inside a function, link it in the header and then call it on load.

function f(){
  var links = document.getElementById("menu").getElementsByTagName("a");

  for(i=0; i<links.length;i++) {
     if(links[i].href == window.location.hash) {
     links[i].className = "active";
      }
  }
}

<body onload="f()">

Also note the var links = ... Meaning a declaration.

1 Comment

Thanks, I tried your suggestion but It doesn't work either. Would you mind reading a comment I wrote to fmgp? I think that's what's causing the problem...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.