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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:23:47+00:00 2026-06-01T18:23:47+00:00

My website uses a hash in its URL to navigate with AJAX (which I

  • 0

My website uses a hash in its URL to navigate with AJAX (which I got from this tutorial). When a link is clicked, a /#/(link name) is added to the end of the url.

If I’m at mysite.com, and I click on “photos”, the url becomes, mysite.com/#/photos, and the “#content” div is re-populated with the #content div from /photos.

Also, if for some reason, the user manually types “mysite.com/photos”, and then clicks on “bio”, his URL will look like “mysite.com/photos/#/bio”

The fact that the page does not re-load, and that the URl does not EXACTLY match the href in the menu a tag, is making it hard for me to figure out how to add an active class to the current menu item.

my menu is generated by wordpress, and looks like this

<div id="main-menu">
<ul>
<li><a href="mysite.com/photos">Photos</li>
etc.
</ul>
</div>

Please help!

  • 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-01T18:23:49+00:00Added an answer on June 1, 2026 at 6:23 pm

    I’m guessing that in mysite.com/photos/#/bio you want to set active to the bio link? In other words, the last “word” of the url.

    To achieve this, you can do something like this:

    var path = window.location.pathname.split("\/"), myString;
    
    if(path[path.length-1] === "")
        myString = path[path.length-2];
    
    else
       myString = path[path.length-1];
    
    myString = myString.toLowerCase()
    console.log(myString);
    

    The if/else check is there because if the pathname is “/hola/bandola/” the last element in the array after split will be an empty string, but if it’s just “/hola/bandola”, “bandola” will be the last item. So to make it work with both ways the check is necessary. Now you have that variable with the right string and can set the right link to active with that information.

    EDIT:
    Since you don’t generate the navigation links dynamically, you’ll have to compare the value you got from my code to something unique for each navigation link. My suggestion is that you add a data-attribute in the a-tags that would match the string you got from my code. Example: <li><a href="mysite.com/photos" data-page="photos">Photos</li>

    Now you go through each a-tag in the UL and compare the data-page values to the value you got from the querystring, and set class to “active” if they match:

    var links = $("#main-menu").find("a");
    $.each(links, function() {
             if($(this).data("page") === myString)
                 $(this).addClass("active");
    }​);​
    

    ​​​You could just have compared the text within the a-tags, but if the text doesn’t match the url-string, that wont work, so it’s better to add a data-attribute. Note that I edited the code above to make sure the “myString” variable is lowercase when comparing to the data-attributes. Hope you understand.

    EDIT2:

    To compare the text inside the a-tag instead, use this each loop instead:

    var links = $("#main-menu").find("a");
    $.each(links, function() {
             if($(this).html("page").toLowerCase() === myString)
                 $(this).addClass("active");
    }​);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a new website moved to my server. This website uses PHP and
I have a website that uses JSP as its view technology (and Spring MVC
I have a website which uses Spring Security. I have realized that when I
My website uses ajax and hashtags for every part of the navigation, so every
My website uses linq-to-sql to load about 50k rows of data from a database.
A plugin which I need to implement in other website uses some jquery functions.
My website uses Forms authentication. I did silverlight 3 module which is designed to
My website uses asual address plugin for implementing hash based URLs. I want to
My web-site has AJAX-powered search, that uses deep-linking. When user follows a link …
I have a website that uses the jQuery Address plugin from Asual for navigation.

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.