Question

Is there a code snippet that can detect the current user's page and then add <a class="active"> to an item in a ul (my menu). I am making a tumblr theme and they do not allow PHP (I usually add <?php if ( $current == "home" ) { echo "class='active'" } ?> in my menu and $current = "home" on my pages) and they do not have a standard (such as current_page_item) so I need to do this with javascript (or jQuery) only. Is it possible?

Was it helpful?

Solution

It's not possible with HTML only but should be straight forward with JQuery, and just a bit harder with javascript only.

Here is a solution relying on JQuery and URL parser plugin: assuming you have the information you need in the file name (e.g: page1.html refered by id="page1")

$(document).ready(function() {
   var pathname = window.location.pathname;
   var page = jQuery.url.attr('file').replace(".html", "");
   $('#' + page).addClass("active");
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top