Created
October 7, 2017 01:49
-
-
Save tomfa/412ac65c35cb976349e85a25c6c4ef1f to your computer and use it in GitHub Desktop.
Bootstrap nav tabs fix
This file contains 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
// 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