Skip to content

Instantly share code, notes, and snippets.

@livingston
Created October 4, 2010 09:39
Show Gist options
  • Save livingston/609456 to your computer and use it in GitHub Desktop.
Save livingston/609456 to your computer and use it in GitHub Desktop.
//Simple tabs with Book-markable urls
var handleTabs = (function (tabHandles) {
if (!tabHandles || !(/[\s]*hasTabs[\s]*/.test(tabHandles.className))) {
return function () {}
}
var tabHandleParent = $(tabHandles.parentNode),
activeClass = 'active',
activateTab = function activateTab(tab) {
var currentTab = $(tab ? document.getElementById(tab) : tabHandles.getElementsByTagName('a')[0]);
if (!currentTab.length) { return }
tabHandleParent.find('.' + activeClass).removeClass(activeClass);
currentTab.parent().addClass(activeClass);
$(document.getElementById(currentTab[0].id + '_content')).addClass(activeClass);
},
handleTabs = function handleTabs() {
var hash = location.hash,
hashString = hash.match(/#\!\/([\S]+)\//),
tabName = (hashString && hashString.length == 2) ? hashString[1] : null;
if (tabName) {
activateTab(tabName);
}
};
handleTabs();
$(window).bind('hashchange', handleTabs);
return handleTabs;
}(document.getElementById('tab-handles')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment