Skip to content

Instantly share code, notes, and snippets.

@shanginn
Created January 13, 2016 08:23
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 shanginn/2e7f93d3856a6cef03b4 to your computer and use it in GitHub Desktop.
Save shanginn/2e7f93d3856a6cef03b4 to your computer and use it in GitHub Desktop.
Make Bootstrap 3 remember which tab they were on. Works only with one set of tabs.
var $tabLinks = $('a[data-toggle="tab"]');
if($tabLinks.length) {
var lastTab = localStorage.getItem('lastTab');
$tabLinks.on('click', function (e) {
//save the latest tab; use cookies if you like 'em better:
localStorage.setItem('lastTab', $(e.target).attr('href'));
});
//go to the latest tab, if it exists:
var $target = lastTab ? $('a[href="' + lastTab + '"]') : $tabLinks.first();
$target.tab('show');
}
var $tb=$('a[data-toggle="tab"]'),LS=localStorage,LT;if($tb.length){$tb.click(function(e){LS.setItem('LBT',$(e.target).attr('href'))});((LT=LS.getItem('LBT'))?$('a[href="'+LT+'"]'):$tb.first()).tab('show');}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment