Skip to content

Instantly share code, notes, and snippets.

@jcutrell
Created June 22, 2010 21:16
Show Gist options
  • Save jcutrell/449103 to your computer and use it in GitHub Desktop.
Save jcutrell/449103 to your computer and use it in GitHub Desktop.
Add current class to links
//works only with static urls, like /home.html, etc
function lockit(someActiveClass){
var href = window.location.href
var split = href.split('/');
if (split[split.length-1] == "") {
var urlend = split[split.length-2]
} else {
var urlend = split[split.length-1]
}
var a = document.body.getElementsByTagName("a");
for (var i = 0; i< a.length; i++){
var asplit = a[i].href.split('/');
if (asplit[asplit.length-1]) {
var aurlend = asplit[asplit.length-2]
} else {
var aurlend = asplit[asplit.length-1]
}
if (aurlend == urlend || aurlend.split('.')[0] == urlend.split('.')[0]{
var yourClass = a[i].className + " " + someActiveClass;
a[i].setAttribute("class", yourClass)
a[i].setAttribute("className", yourClass);
}
}
}
lockit("superAwesomeClassname");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment