Need help fixing navigation menu
I am using JQuery for a navigation menu which adds a "selected" class to its links depending on which page is active.
This is what the HTML code for the menu looks like:
- <div id="nav">
- <ul>
- <li><a href="index.php">HOME</a></li>
- <li><a href="gallery.php?categoryID=1">GALLERIES</a></li>
- <li><a href="news.php">NEWS</a></li>
- <li><a href="links.php">LINKS</a></li>
- <li><a href="contact.php">CONTACT US</a></li>
- </ul>
- </div>
And this is the JQuery code:
- var pageLocation = window.location.toString().split("/");
- var pageName = pageLocation[pageLocation.length - 1];
- var selectedLink = $("li:has(a[href="+pageName+"])");
-
- $("a[href="+pageName+"]").addClass("selected");
Now this works fine if you click on one of the links in the #nav div, but if you click on another link which is not in the #nav div then the menu becomes broken.
For example, if you go to my test site here:http://www.awais-muzaffar.co.uk/test/index.php and then click on galleries, and then click on the 12 Days of Christmas link below, you will observe that the navigation menu becomes broken.
How do I get around an issue like this?