Skip to content

Instantly share code, notes, and snippets.

@johnkary
Created June 16, 2010 20:27
Show Gist options
  • Save johnkary/441220 to your computer and use it in GitHub Desktop.
Save johnkary/441220 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
//Add 'ku_currentlinks' class to links in #leftnavigation if href matches exactly
currentnavnew();
//Add 'current' class to links in #leftnavigation if matches directory or a file in that directory
currentnavnew('leftnavigation', 'current');
//Add 'current' class to #tabbed_navigation links if href match exactly
currentnavnew('tabbed_navigation', 'current', true);
/**
* Checks all links within an HTML node and compares each link's href
* with the current URL path. If a match exists, a CSS class is added to
* the link tag.
*
* Can match strict (path must match exactly) or nonstrict (path matches if
* within the directory of the link.)
*
* Use nonstrict match when linking to directories (e.g. top-level navigation.)
* This is the default. Use strict match when linking to exact files
* (e.g. tabbed navigation.)
*
* URL Query Strings (e.g. file.shtml?foo=bar) and Anchors (file.shtml#foo) are
* NOT matched.
*
* @param string containerId HTML id of node containing all links to
* check. Defaults to 'leftnavigation'
* @param string cssClass CSS class to add if match exists. Defaults
* to 'ku_currentlink'
* @param boolean strict Match exact filename or just directory.
*/
function currentnavnew(containerId, cssClass, strict)
{
//Set defaults
if(undefined === containerId) {
containerId = 'leftnavigation';
}
if(undefined === cssClass) {
cssClass = 'ku_currentlink';
}
if(undefined === strict) {
strict = false;
}
//Delegate
if(true === strict) {
strictMatch(containerId, cssClass);
} else {
nonstrictMatch(containerId, cssClass);
}
return;
}
/**
* Assigns CSS class to links within containerId that match the current path.
*
* @param string containerId HTML id of node containing links to check
* @param string cssClass CSS class to add if match exists
*/
function strictMatch(containerId, cssClass)
{
var path = location.pathname;
if(path){
$kuj(document).ready(function() {
$kuj('#' + containerId + ' a[href="' + path + '"]').addClass(cssClass);
});
}
}
/**
* Assigns CSS class to links within containerId that match the current path or
* the link's most granular directory.
*
* @param string containerId HTML id of node containing links to check
* @param string cssClass CSS class to add if match exists
*/
function nonstrictMatch(containerId, cssClass)
{
var path = location.pathname;
if(path){
$kuj(document).ready(function() {
$kuj('#' + containerId + ' a').each(function(index) {
//Find link's href and make it into a regex
var escapedHref = $kuj(this).attr("href");
var escapedHrefRegex = new RegExp(escapedHref + '([\\w\-]+\.[a-z]{2,5})?$', "gi");
//Check if link's href matches the current path
if(path.match(escapedHrefRegex)){
//Add a CSS class to this link
$kuj(this).addClass(cssClass);
}
});
});
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment