Skip to content

Instantly share code, notes, and snippets.

@masciugo
Created March 1, 2018 11:00
Show Gist options
  • Save masciugo/a33078b9db9cd05c2f2d99d68e281c16 to your computer and use it in GitHub Desktop.
Save masciugo/a33078b9db9cd05c2f2d99d68e281c16 to your computer and use it in GitHub Desktop.
URL aware of active tab
$(() => {
if (location.hash) {
$("a[href='" + location.hash + "']").tab("show"); // go to tab when hash is present in the url
}
$(document.body).on("click", "a[data-toggle]", function(event) {
location.hash = this.getAttribute("href"); // add hash to url when change tab
});
});
$(window).on("popstate", function() { // make history navigation aware of anchor
var anchor = location.hash || $("a[data-toggle='tab']").first().attr("href");
$("a[href='" + anchor + "']").tab("show");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment