Skip to content

Instantly share code, notes, and snippets.

@rutger1140
Created September 27, 2011 13:57
Show Gist options
  • Save rutger1140/1245104 to your computer and use it in GitHub Desktop.
Save rutger1140/1245104 to your computer and use it in GitHub Desktop.
An old javascript I used for setting active menu classes (with @ppk his W3CDOM checker)
var W3CDOM = (document.getElementsByTagName && document.createElement);
function init(){
if(!W3CDOM) return;
setActiveMenuItem();
}
function setActiveMenuItem(){
menuobject = document.getElementsByTagName('li');
for(var j=0; j<menuobject.length; j++){
menuAObject = menuobject[j].getElementsByTagName('a');
for(var s=0; s<menuAObject.length; s++){
if(menuAObject[s] == self.location.href)
menuAObject[s].className+=' active';
}
}
}
window.onload=init;
@rutger1140
Copy link
Author

This is obviously more for reference sake, than to show off my l33t skills =]

@rutger1140
Copy link
Author

Currently I use this one, with jQuery (by Remy Sharp - http://docs.jquery.com/Tutorials:Auto-Selecting_Navigation)

$(function(){
  /* Find active menu item */
  var path = location.pathname.substring(1);
  if(path) 
    $('#secondary a[href$="' + path + '"]').parent("li").attr('class', 'active');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment