Skip to content

Instantly share code, notes, and snippets.

@atitsbest
Created July 7, 2015 08:13
Show Gist options
  • Save atitsbest/e76a396cb80f9e465aa3 to your computer and use it in GitHub Desktop.
Save atitsbest/e76a396cb80f9e465aa3 to your computer and use it in GitHub Desktop.
Bootstrap Tab Url Hash
// Javascript to enable link to tab
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;
})
@ggonzal
Copy link

ggonzal commented May 22, 2018

for bootstrap 4 (twbs/bootstrap#25220):
// Show appropriate pill based on #anchor in URL
var url = window.location.href;
if (url.indexOf("#") > 0){
var activeTab = url.substring(url.indexOf("#") + 1);
$('.nav[role="tablist"] a[href="#'+activeTab+'"]').tab('show');
}

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