0

I'm using a navigation bar, and I want to have an active class on elements when they're on the element's page.

For instance, I'd need jQuery to check the url of the current page, and if the url is http://supjamess.tumblr.com/, it'd add the class active to the a element .index, http://supjamess.tumblr.com/ask & it'd add the class active to the a element .ask, and so on.

If anyone could provide a code it'd be extremely helpful.

2
  • *chose, and his decision is not my fault, thanks for the minus 2. Immature... Commented Jul 6, 2012 at 22:53
  • I simply offered my solution to the problem based on the way that I would solve it. I also explained why I opted to use this method in the comments of my answer. You are entitled to your opinion, but that does not mean that I am wrong, nor do I deserve a downvote. Commented Jul 6, 2012 at 23:00

2 Answers 2

2

My example requires jQuery:

if ( window.location.pathname == '/' ) {
    var url_class = 'index';
} else {
    // Very first part of the path, so /path/to/url returns 'path'
    var url_class = window.location.pathname.split('/');
    url_class = url_class[1];
}

$('a.' + url_class).addClass('active');

What this basically does, is get the pathname component of the URL (After the domain and before the query string, see here) and then checks to see if it is '/', aka index and manually sets the URL class to 'index'.

Otherwise, it gets the first component of the URL, so /ask returns ask, /questions/bar returns questions, /users/edit/5 returns users, /tagged/layouts returns tagged, etc.

Then jQuery adds the class active to any element with the given class.

Sign up to request clarification or add additional context in comments.

5 Comments

You can't use "class" as the name of a variable in Javascript, it thorws an error in most browsers
@IvanCastellanos Yep, fixed it before you commented :)
I don't get how this works, could you please explain it & how it would work with /ask, /tagged/layouts, etc? @RogueCoder
@JamesCharless Check my post, I've updated it for more detail
1

switch based off of location.pathname

 if (location.pathname == "/") {
      $("a.index").addClass("active");
 } else if (location.pathname == "ask") {
      $("a.ask").addClass("active");
 }
 ... and so on

additional pages are added with additional else if statements, so the code becomes:

 if (location.pathname == "/") {
      $("a.index").addClass("active");
 } else if (location.pathname == "/ask") {
      $("a.ask").addClass("active");
 } else if (location.pathname == "/tagged/layout") {
      $("a.layout").addClass("active");
 } else if (location.pathname == "/YOUR PATH") { // to add aditional pages, replace CAPS
      $("a.CLASS OF NAV LINK").addClass("active");
 }

You can try out and experiment with javascript on jsfiddle.net, or in your own console (f12 in most browsers)

3 Comments

True, this is similar to the way that I use to route my URLs in Backbone apps. The benefit is that if I need to have something the same for a directory, I can employ search(), like so: if (location.pathname.search(/^\/admin/ === 0)) { admin section }
@Austin would you be able to finish this code for me? I'm new to javascript and I don't quite get it. / is a.index, /ask is a.contact, /tagged/themes is a.layouts and that's it. Thank you so much!!
See the revised answer for clarification

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.