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 6229315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:32:45+00:00 2026-05-24T09:32:45+00:00

Javascript function wchich I am using is selecting more than one link. It is

  • 0

Javascript function wchich I am using is selecting more than one link. It is doing that because I used Regular expression with ‘^’ symbol. I have done it this way because my links looks like that:

http://localhost/MainApp/User/UserEdit.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1

and value of location.pathname is /MainApp/User/UserEdit.aspx
And so I thought that I will check only begining of link and it will work well.

So my Javascript function looks like that:

function markActiveLink() {
    var path = location.pathname;
    var links = null;
    if (path) {
        links = $("a[href^='" + path + "']");
    } else {
        links = $("a[href='/']");
    }
    links.parents("li").each(function () {
        $(this).addClass('current').closest('li').addClass('current');
    });
}

This function is working quite well and adding css class “current” to <li> elements and it’s parent <li> element.

Problem: I have also links that differ only by the ending. And this function trims endings. Those links looks like that:

http://localhost//MainApp/User/Order.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=NMO

and http://localhost//MainApp/User/Order.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO

So they differ only by the ending -> MNO and MO. And when I am selecting one of this links my function is adding css class to them both.

I have tried using document.location.href instead of document.pathname like that:

      function markActiveLink() {
var path = document.location.href;        
            var links = null;
            if (path) {
                links = $("a[href='" + path  + "']");
            } else {
                links = $("a[href='/']");
            }
            links.parents("li").each(function () {
                $(this).addClass('current').closest('li').addClass('current');
            });
        }

But then none of links were selected.

Code of some of menu links look like:

<ul>
    <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserDetails.aspx?id=<%=pe.UserId%>&from=user">        
        <%=Me.GetLocalResourceObject("CurrentUser.Text")%></a>
        <ul>
            <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserDetails.aspx?id=<%=pe.UserId%>&from=user">
                <%=Me.GetLocalResourceObject("CurrentUser.Text")%>
            </a></li>
            <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserOrder.aspx?id=<%=pe.UserId%>&type=NMO">
                <%=Me.GetLocalResourceObject("NMOrders.Text")%>
            </a></li>
            <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserOrder.aspx?id=<%=pe.UserId%>&type=MO">
                <%=Me.GetLocalResourceObject("MOrders.Text")%>
            </a></li>
            <li><a href="<%=System.Configuration.ConfigurationManager.AppSettings("VirtualDirectory").ToString()%>User/UserPage.aspx?id=<%=pe.UserId%>">
                <%=Me.GetLocalResourceObject("UserPage.Text")%>
            </a></li>
        </ul>   
    </li>
    [...]
</ul>

Menu structure looks like that:

<ul>
    <li><a></a>
        <ul>
            <li><a></a></li>
            ...
            <li><a></a></li>
        <ul>
    </li>
    ...
    </li>
    <li><a></a>
        <ul>
            <li><a></a></li>
            ...
            <li><a></a></li>
        <ul>
    </li>   
</ul>

And on page those links source code looks like that:

<ul>
                <li><a href="/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=NMO">
                    User Orders NMO
                </a></li>
                <li><a href="/App/User/UserOrder.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1&type=MO">
                    User Orders MO
                </a></li>
                <li><a href="/App/User/UserPage.aspx?id=949abc91-a644-4a02-aebf-96da3ac7d8e1">
                    User Page
                </a></li>
</ul>

Any help here much appreciated!

  • 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-05-24T09:32:46+00:00Added an answer on May 24, 2026 at 9:32 am

    Try function like that:

    function markActiveLink() {
        var path = location.href.replace(/^.*\/\/[^\/]+/, '');
        var links = null;
        if (path) {
            links = $("a[href='" + path + "']");
        } else {
            links = $("a[href='/']");            
        }
        links.parents("li").each(function () {
            $(this).addClass('current').closest('li').addClass('current');
        });          
    
    }
    

    It should do the trick.

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

Sidebar

Related Questions

I have function in Javascript which works fine using prototype. The function is used
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
I have a function that's called by a link which should check various checkboxes
I have a little javascript function which is attached to an onClick event of
I have a JavaScript function which is quite long and performs a number of
i got a client side javascript function which is triggered on a button click
During my routine work, i happened to write the chained javascript function which is
We have a JavaScript function named move which does just windows.location.href = any given
I have a javascript function for checking errors which I am calling on OnClicentClick
I would like a JavaScript function to have optional arguments which I set a

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.