Skip to content

Instantly share code, notes, and snippets.

@Nemo64
Created July 18, 2014 15:17
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 Nemo64/05cd841d886b6de405fb to your computer and use it in GitHub Desktop.
Save Nemo64/05cd841d886b6de405fb to your computer and use it in GitHub Desktop.
remember which bootstrap tab was active while navigating. This is a very cheap cookie trick and it requires jquery-cookie.
// behavior for tabs
// FIXME can create unexpected behavior with multiple tabs on one page, also with multiple browser tabs/windows
$(document).on('click', '[data-toggle="tab"], [data-toggle="pill"]', function(event) {
var hash = $.prop(this, "hash");
$.cookie('active-tab', hash, { path: '/' });
});
$(window).on('popstate hashchange ready load', function() {
var cookieTab = $.cookie('active-tab');
var tab = cookieTab || null;
if (tab !== null) {
$('a[href="' + tab + '"]').tab('show');
}
});
// if any tab has an errornous form type display it
$(window).on('ready load', function() {
var $tabs = $('.tab-content');
$tabs.on('invalid.tabs', '.tab-pane[id]', function () {
$('a[href="#' + this.id + '"]').tab('show');
});
// trigger invalid on the first error
$tabs.find('.has-error:first').trigger('invalid.tabs');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment