Skip to content

Instantly share code, notes, and snippets.

@aeurielesn
Created February 22, 2012 21:13
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 aeurielesn/1887310 to your computer and use it in GitHub Desktop.
Save aeurielesn/1887310 to your computer and use it in GitHub Desktop.
Modified jquery tab plugin to allow subtabs
$(".tabs").tabs();
(function($) {
var defaults = {
tabs: "div.tab",
expire: null
};
$.fn.tabs = function(options) {
var counter = 1;
var tabSave = {};
var hashStr = window.location.hash.replace('#', '');
var args = hashStr.split(';');
for (var val in args) {
if (args[val].replace(/s\g/, '') != '') {
var vals = args[val].split('=');
tabSave[vals[0]] = vals[1];
}
}
function saveTabs() {
var str = '';
for (var prop in tabSave) {
str += prop + '=' + tabSave[prop] + ';';
}
window.location.hash = str;
}
function getTabs(tab) {
var str = window.location.hash;
var params = str.split(';');
for (var val in params) {
var vals = params[val].split('=');
if (vals[0].replace('#', '') == tab) return vals[1];
}
return false;
}
return this.each(function() {
var opts = $.extend({}, defaults, options);
var tis = $(this);
var tabs = $(this).find(opts.tabs);
var hash = getTabs("tab_" + counter);
if (hash) {
tabs.hide();
tis.find('div.tab:eq(' + hash + ')').show();
tis.find('> ul.tabmenu li:eq(' + hash + ')').addClass('selected');
} else {
tabs.hide().filter('div:first').show();
tis.find('> ul.tabmenu li:eq(0)').addClass('selected');
}
tis.find('> ul.tabmenu a').click(function(e) {
tabs.hide();
tabs.filter(tabs.eq($(this).parent().index())).show();
tis.find('> ul.tabmenu li').removeClass('selected');
$(this).parent().addClass('selected');
tabSave["tab_" + tis.index()] = $(this).parent().index();
saveTabs();
e.preventDefault();
});
counter++;
});
};
})(jQuery);​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment