I am trying to write a JS code to change color of the current link. For example, if page one address is www.abc.com/abc, and page 2 is www.abc.com/abc/product, then page one will turn red.Basically if page 2 is subpage of page1, then page 1 will turn red. Here is my idea:
compare char one by one in page1 and page2
if(currentpage.href!=one of a.href)
flag=false;
if(flag==true)
then turn red
else
then turn blue
Here are my codes below:
<div id="changeColor" class="horizontalcssmenu" style="padding-left:7px;">
<a href="linkeadress" >HOME</a>
<a href="linkaddress" >SHOP</a>
</div>
<script type="text/javascript">
var links = document.getElementById("changeColor");
var a = links.getElementsByTagName("a");
var thisLocationHref = window.location.href;
var counter=0;
for(var i=0;i<a.length;i++){
var flag="true";
var tempLink=a[i];
while(counter<=a[i].length){
if(thisLocationHref[counter]!=tempLink.href[counter])
{flag="false";}
counter++;
}
if(flag=="true")
{tempLink.style.color=red";
}
else
{
tempLink.style.color="blue";
}
}
Thank you for time!