Last active
October 13, 2015 23:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} ) | |
} ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple script to enhance bootstrap tabs for situations where
Hope it helps someone. Ty.