For my website navigation I need to add the class 'active' to the li element depending on if it matches the current URL.
Nav HTML:
<ul id="nav">
<div id="wrapper">
<li><a href="/">Home</a></li>
<li><a href="/tagged/Review">Reviews</a></li>
<li><a href="/tagged/First_Look">First Looks</a></li>
<li><a href="/tagged/Commentary">Commentaries</a></li>
<li><a href="/tagged/Walkthrough">Walkthroughs</a></li>
<li><a href="/tagged/Achievement">Achievements</a></li>
</div>
</ul>
If you want to use "pure" ("vanilla") JavaScript, use the following code(assuming that
<ul id="nav">
exists):Using jQuery:
I think, in this case, is better change in server side.
Using javascript you can do:
Put the code after loaded DOM.
If jquery, you can use:
EDIT
window.onload or $.ready is the way to know if the document is loaded.