What is the best way of making these tabs persist?
http://twitter.github.com/bootstrap/javascript.html#tabs
To add some context, this is for a rails application. I'm passing an array [tab1, tab2] to my view, rendering both tabs and using the bootstrap tab plugin to toggle their visibility.
I wanted to improve the best answer here..
Credit goes to Sucrenoir, but if you want to avoid jumping on the page when you change tabs, use this improved code:
Another modified version if you don't want tab clicks to be added to the history, but also don't want the page jumping up and down:
Execute the following code after DOM load:
However, this leads to poor UI experience, since currently active tab will shown first, and then it will be switched to a tab from location.hash
Tested for Bootstrap 4, minimalist (2 lines) code without history push, that works on any page with nav-tabs.
You can get the URL fragment (that's the part of the URL after
#
) on load usingwindow.location.hash
, and specifically set that tab as visible:I wanted to improve the best two answers here.. :)
Credit goes to Sucrenoir and d-wade.
Because in code is used history API you can't use onchangehash (https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange). This code add the functionality of back button (https://developer.mozilla.org/cs/docs/Web/API/WindowEventHandlers/onpopstate).