None of these worked for me. My Bootstrap setup navigates to separate pages (so I can't do it on a click action, but the active class is removed on the navigation to a new page), and my urls don't match exactly. So here's what I did, based on my exception-based situation. Hope it helps others:
//Adding the active class to Twitter bootstrap navs, with a few alternate approaches
$(document).ready(function() {
var rawhref = window.location.href; //raw current url
var newpage = ((window.location.href.match(/([^\/]*)\/?$/)[1]).substring(1)); //take only the last part of the url, and chop off the first char (substring), since the contains method below is case-sensitive. Don't need to do this if they match exactly.
if (newpage == 'someNonMatchingURL') { //deal with an exception literally
newpage = 'matchingNavbarText'
}
if (rawhref.indexOf('somePartofURL') != -1) { //look for a consistent part of the path in the raw url to deal with variable urls, etc.
newpage = "moreMatchingNavbarText"
}
$(".nav li a:contains('" + newpage + "')").parent().addClass('active'); //add the active class. Note that the contains method requires the goofy quote syntax to insert a variable.
});