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

  • SEARCH
  • Home
  • 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 959119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:58:30+00:00 2026-05-16T00:58:30+00:00

I have an ASP.NET website, on the masterpage I have a basic navigation menu:

  • 0

I have an ASP.NET website, on the masterpage I have a basic navigation menu:

<ul id="menu">
    <li><a href="Default.aspx"><span>Home</span></a></li>
    <li><a href="Services.aspx"><span>Services</span></a></li>                       
    <li><a href="HowItWorks.aspx"><span>How It Works</span></a></li>
    <li><a href="ContactUs.aspx"><span>Contact Us</span></a></li>
</ul>

My goal is to add a JavaScript function on the masterpage that will dynamically add the “active” class to the tag that represents the page the user is on. For example, if the user is on http://www.mywebsite.com/default.aspx, than the attribute [class=’active’] should be added to the first link in the navbar. The active class basically just highlights the link so the user knows which page they are currently on.

Here is my JavaScript code:

<script type="text/javascript">
    $(document).ready(selectMenuItem);

    function selectMenuItem() {
        var name = getPageName();
        if (name)
            $('#menu a[href="' + name + '"]').addClass("active");
    }

    function getPageName() {
        var path = window.location.href.split('?')[0];
        var name = path.substring(path.lastIndexOf('/') + 1, path.length);
        return name;
    }
    </script>

Here is what happens, when the user visits any page on the site, the selectMenuItem() function executes. The function makes a call to getPageName() which returns the name of the page file that the user is viewing (ex: “default.aspx”). The function uses this name string to find which tag links to the page the user is currently on, when found the class is correctly added.

This code works great, however the process is case sensitive. Basically I’m at the mercy of the user. If they manually type in “mywebsite.com/DefUlT.aspx” than my code will fail because the casing in the substring “default.aspx” does not match the one in the tag. I could solve this problem by telling the JavaScript function to force the name string to be lower case, however this would mean that whenever I add new menu item I need to be sure I write the href attribute all in lower case, which isn’t a big deal but it’s not elegant enough for me. It wouldn’t be a problem if I was the only one working on this site, but I will hand the work of to a number of other professionals and I feel stupid telling everyone, “be sure the href values are all lowercase or the code will break!”

Can someone interested in a challenge maybe tell me how to work around this? I’m extremely new to jQuery (started last night) so I’m sure there’s an elegant solution that involes changing this line of code:

$(‘#menu a[href=”‘ + name + ‘”]’).addClass(“active”);

My problem is that I need the “href” attribute to be turned to lower case before the comparison is made to the name variable, I would then just perform “name.toLowerCase()” so that I know the case differences between the href attribute and name variable are irrelevant. Is there an equivalent jQuery selector which allows me to specify an expression perhaps?

Thanks,

-Andrew C.

  • 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-16T00:58:31+00:00Added an answer on May 16, 2026 at 12:58 am

    I don’t know of a way to do it with a selector, but looping through all your menu links as in the following, you could apply the toLowerCase() method to both the href and name, so you’d know the match would work, no matter what the user entered in the URL:

    function selectMenuItem() {
        var name = getPageName();
        if (name){
            $('#menu a').each(function(){
                if($(this).attr('href').toLowerCase() == name.toLowerCase()){
                    $(this).addClass('active');
                    return;
                }
            });
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you have both heads in your repository (remember, you… May 16, 2026 at 2:21 pm
  • Editorial Team
    Editorial Team added an answer for /f "tokens=*" %x in (a.txt) do @echo %x The… May 16, 2026 at 2:21 pm
  • Editorial Team
    Editorial Team added an answer A handle is an indirect way to reference an object… May 16, 2026 at 2:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a javascript function in the default.aspx page of my asp.net website project.
TGIF, I have a website I'm developing which is using ASP.NET masterpage/sitemap/content pages setup.
I am writing a website with Visual Studio 2008 and ASP.NET 3.5. I have
I have an asp.net website in my production server which running fine now. Now
I have a an ASP .Net MVC website which works fine when testing locally.
I'm working on a ASP.NET website that on some requests will run a very
This is the connection string that gets generated with the New ASP.Net Website template
I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into
What are your techniques to using mobile with asp.net, I know how to detect
Looking for some direction here as I'm running into some migration problems. We have

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.