Last active
October 15, 2022 02:13
-
-
Save josheinstein/5586469 to your computer and use it in GitHub Desktop.
Handle back button issues with Twitter Bootstrap's tab component.
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
// 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)); | |
} | |
}); | |
}); |
This is awesome! Thanks, @josheinstein -- and @poddys for the Bootstrap 3.x fix.
New fork now puts together this work and François Zaninotto's
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, thank you, thank you!!