Skip to content

Instantly share code, notes, and snippets.

@tomfa
Created October 7, 2017 01:49
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 tomfa/412ac65c35cb976349e85a25c6c4ef1f to your computer and use it in GitHub Desktop.
Save tomfa/412ac65c35cb976349e85a25c6c4ef1f to your computer and use it in GitHub Desktop.
Bootstrap nav tabs fix
// This allows you to use <a href="#id-of-tab"> as well as handle direct links and back button (popstate)
(function(){
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
}
// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
});
// Fix hash links
$('a[href^="#"]').click(function (e) {
var url = this.href;
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
}
});
// Fix popstate
window.addEventListener("popstate", function(e) {
$('.nav-tabs a[href="' + location.hash + '"]').tab('show');
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment