We want to add class on a link depending on window.location.pathname
our html is:
<nav>
<a href="index.php">Home</a>
<a href="company.php">Company</a>
<a href="products.php">Products</a>
<a href="technical.php">Technical</a>
<a href="services.php">Services</a>
<a href="contactus.php">Contact Us</a>
</nav>
and the document URL is http://www.nicadpower.com/index.php and so we did
var page_name = window.location.pathname.substring(1);
after getting/knowing the pathname to be ‘index.php’ we want jquery to do it’s tricks comparing the ‘page_name’ to href and if it’s equal would add a class=”current”
making our html code to be like this
<nav>
<a href="index.php" class="current">Home</a>
<a href="company.php">Company</a>
<a href="products.php">Products</a>
<a href="technical.php">Technical</a>
<a href="services.php">Services</a>
<a href="contactus.php">Contact Us</a>
</nav>
That will select the
aelement with the requiredhrefattribute value, and add a class to it. See example fiddle here.