I am trying to have a page that will highlight - in the navigation bar - the page you are currently on. What I want is a javascript that will extract the page's URL as a string, and if it matches the url in the href, it will apply the class .current to it.
I found a way that looks like it should do exactly this, at http://blog.richnetapps.com/index.ph...&c=1&tb=1&pb=1 and it seems like it should work, but... it doesn't.
Can anyone tell me what I've done wrong? Thanks. BTW, I've also tried calling the script in a onload function, but it still didn't work.
Code:<html> <head> <style> .current {background-color:red;} </style> </head> <body> <script language="text/javascript"> function extractPageName(hrefString) { var arr = hrefString.split('.'); arr = arr[arr.length-2].split('/'); return arr[arr.length-1].toLowerCase(); } function setActiveMenu(arr, crtPage) { for(var i=0; i < arr.length; i++) if(extractPageName(arr[i].href) == crtPage) { arr[i].className = 'current'; arr[i].parentNode.className = 'current'; } } function setPage() { if(document.location.href) hrefString = document.location.href; else hrefString = document.location; if (document.getElementById('navbar')!=null) setActiveMenu(document.getElementById('navbar').getElementsByTagName('a').extractPageName(hrefString)); } </script> <div id="navbar"> <a href="one.htm">one</a> <a href="two.htm">two</a> <a href="three.htm">three</a> </div> <script language="text/javascript">setPage()</script> </body> </html>



Reply With Quote


Bookmarks