Skip to content

Instantly share code, notes, and snippets.

@fzaninotto
Created May 17, 2012 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fzaninotto/b67783284c5eaa21815c to your computer and use it in GitHub Desktop.
Save fzaninotto/b67783284c5eaa21815c to your computer and use it in GitHub Desktop.
$(document).ready(function() {
// add a hash to the URL when the user clicks on a tab
$('a[data-toggle="tab"]').on('click', function(e) {
history.pushState(null, null, $(this).attr('href'));
});
// navigate to a tab when the history changes
window.addEventListener("popstate", function(e) {
var activeTab = $('[href=' + location.hash + ']');
if (activeTab.length) {
activeTab.tab('show');
} else {
$('.nav-tabs a:first').tab('show');
}
});
});
@abrugh
Copy link

abrugh commented Jun 13, 2013

I was having trouble with this gist not reverting to a tab with no hash (the first/default tab). I got it working by changing line 9 to read:

if (location.hash.length){

The way it is in the gist, activeTab.length is always 1 as activeTab is a list with one object in it so it never makes it into the else block.

I realize this is a year old now and things may have changed since then, just trying to help out fellow googlers in the future.

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