public
Last active

Handle back button issues with Twitter Bootstrap's tab component.

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Handle back button issues with Twitter Bootstrap's tab component.
// Based on: http://stackoverflow.com/a/10120221/81769
// It has been changed to avoid the following side effects:
// - Switching tabs was being added to navigation history which is undesirable
// (Worked around this by using location.replace instead of setting the hash property)
// - Browser scroll position was lost due to fragment navigation
// (Worked around this by converting #id values to #!id values before navigating.)
$(document).ready(function () {
 
if (location.hash.substr(0,2) == "#!") {
$("a[href='#" + location.hash.substr(2) + "']").tab("show");
}
 
$("a[data-toggle='tab']").on("shown", function (e) {
var hash = $(e.target).attr("href");
if (hash.substr(0,1) == "#") {
location.replace("#!" + hash.substr(1));
}
});
 
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.