Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@igal
Created February 14, 2010 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igal/304053 to your computer and use it in GitHub Desktop.
Save igal/304053 to your computer and use it in GitHub Desktop.
JavaScript URL-based menu activation code
// USAGE: This code parses the browser's URL and displays either the matching menu section or an error if one couldn't be found.
// Associative array of menu items to activate and the regular expression
// fragments that activate them:
var menu_items_and_activation_patterns = {
"about": "about",
"attend": "(attend|register)",
"blog": "(blog|archive)",
"schedule": "events/.+?/schedule",
"sessions": "events/.+?/sessions",
"proposals": "events/.+?/proposals",
"speakers": "events/.+?/speakers",
"sponsors": "sponsors"
}
var pathname = window.location.pathname;
// Uncomment line below to set a specific pathname for development:
// var pathname = "/events/2009/speakers";
var menu_item_matched = null;
for (var menu_item in menu_items_and_activation_patterns) {
var pattern = menu_items_and_activation_patterns[menu_item];
var re = new RegExp("^/" + pattern + "(?:/.*)?$");
if (pathname.match(re)) {
menu_item_matched = menu_item;
break;
}
}
if (menu_item_matched) {
alert("Matched menu item: " + menu_item);
} else {
alert("No match for pathname: " + pathname);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment