0

I have this

if( window.location.href == "http://localhost:3000/categories") {
     $("#container ul #all_categories a.categories-menu").css("font-weight","bold");
    }   
});

but I have a url with:

http://localhost:3000/username/boards

username is dynamic username e.g.

http://localhost:3000/michael-21/boards
http://localhost:3000/Richard_10/boards
http://localhost:3000/Mary.50/boards

and change for each user.

How can do the same functionality but with this last url?

Thank you

1
  • 1
    Your selector doesn't need multiple IDs, this will work and probably be faster (ever so slightly): $("#all_categories .categories-menu"). Commented Mar 2, 2012 at 18:35

1 Answer 1

1

You don't need a RegEx. The following method is easier, and is also easier to maintain (you don't have to change the URL in the code when moving the page to the production environment):

location.pathname.indexOf('/boards', 1) !== -1

The previous would also match /me/not/boards. If you don't want that:

var index = location.pathname.indexOf('/boards', 1);
index !== -1 && location.pathname.lastIndexOf('/', index-1) === 0

Finally, the RegEx method:

/^\/[^/]+\/boards/.test(location.pathname)

Implementation example

    if( /^\/[^/]+\/boards/.test(location.pathname) ) {
         $("#container ul #all_categories a.categories-menu").css("font-weight","bold");
    }   
});
Sign up to request clarification or add additional context in comments.

2 Comments

Can you write a example for this url http://localhost:3000/michael-gray/boards with conditional and add class? Thank you Rob W
Wouu @RobW works fine. Thank you very much!. excuse me for my delay in answering. Thank you again :). How could works for localhost:3000 or localhost, or any domain index, without any path after .com domain? Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.