Skip to content

Instantly share code, notes, and snippets.

@warrendodsworth
Last active October 13, 2015 23:48
Show Gist options
  • Save warrendodsworth/e2704b0f9a7d61ddf25c to your computer and use it in GitHub Desktop.
Save warrendodsworth/e2704b0f9a7d61ddf25c to your computer and use it in GitHub Desktop.
Recovering the last known Tab (Bootstrap Tabs) on returning to the page using localStorage, can use session storage too
// Bootstrap Tab - Return to Last Known on page refresh or back/forward movement.
// Using data- attrs to identify tab groups to enable use multiple times on a page
$( '[data-tab-group]' ).each( function ( i, o ) {
var tabGroup = $( o );
var lastKnownTabId = tabGroup.data( 'tab-group' ); // Tab Group Id, from the data- attr
var lastKnownTab = localStorage.getItem( lastKnownTabId );
//Set last known tab back on script load - specific to this Tab Group Id
tabGroup.find( ' a[href="#' + lastKnownTab + '"]' ).tab( 'show' );
//Update Tab Group Id in Local storage
tabGroup.find( 'a[data-toggle="tab"]' ).on( 'shown.bs.tab', function ( e ) {
localStorage.setItem( lastKnownTabSession, e.target.toString().split( "#" )[1] ); // activated tab
} )
} )
@warrendodsworth
Copy link
Author

A simple script to enhance bootstrap tabs for situations where

  • a user comes back to a tabbed page frequently
  • a user is using a paging control inside a tab - which involves a page refresh.

Hope it helps someone. Ty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment