Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:16:27+00:00 2026-06-13T11:16:27+00:00

I am trying to write a JS code to change color of the current

  • 0

I am trying to write a JS code to change color of the current link. For example, if page one address is http://www.abc.com/abc, and page 2 is http://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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:16:28+00:00Added an answer on June 13, 2026 at 11:16 am

    It is really not possible to determine if a link actually IS pointing to a “parentpage” or a “subpage”, but according to the examples given you can try to evaluate if the current page
    is a “subpage” of a link by their lengths and the current page prepends the link.

    <div id="changeColor" class="horizontalcssmenu" style="padding-left:7px;">
    <a href="http://localhost">HOME</a> <!-- www.abc.com/abc -->
    <a href="http://localhost/products/">SHOP</a> <!-- www.abc.com/abc/prodct -->
    </div>
    
    <script type="text/javascript">
    var links=document.getElementsByTagName('a');
    for (var i=0;i<links.length;i++) {
        //is length of the link less than current page?
        if ((parseInt(links[i].href.length)<parseInt(window.location.href.length)) && 
                //does the link prepending the current page?
                (window.location.href.indexOf(links[i].href)>-1)) { 
                    //probably this is a link to the "parentpage", eg "page 1"
                    links[i].style.color='#ff0000';
        }
    }
    </script>
    

    Please – i really dont want to be downvoted for this. It does what the question angles for, strange questions gives strange answers 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a javascript code that would change the value of
I'm trying to change the color on a white marker image by code. I
I m trying write code that after reset set up rrpmax as 3000. It
I'm trying to write code in my controller that when run, will create a
I am trying to write code for getting my cords on the mobile. I
I'm trying to write code that will load an image from a resource, and
I've been trying to write code that loads a .png file, attaches hotspot information,
Alright, here I am again trying to write code from scratch and I can't
Trying to write a code at the moment that basically tests to see if
I'm trying to write a code to actually sort my array in an ascending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.