Created
March 1, 2018 11:00
-
-
Save masciugo/a33078b9db9cd05c2f2d99d68e281c16 to your computer and use it in GitHub Desktop.
URL aware of active tab
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
$(() => { | |
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