1

So I am trying to figure out why my page loads me back to the default tab after being refreshed.

I want to stay in the current tab even after the page is refreshed/reloaded

user1535882
  • 119
  • 1
  • 3
  • 12

2 Answers2

2

It will most likely be due to your tabs being loaded on the client side.

This means each time you switch to a different tab, your not actually making a new request, only showing and hiding different tabs. If you reload tabs, it will default back to first tab.

Easiest and most common way around this would be to use a URL hash to track the user's active tab. Then when you're loading your tabs after each request, can check the URL for a hash and if one exists, show the corresponding tab.

More info here: http://www.w3schools.com/jsref/prop_loc_hash.asp

Hope this helps :)

timothyclifford
  • 6,799
  • 7
  • 57
  • 85
0

It was the easiest way to stay the tab. Look at the code like below. Copy this code and paste in your js file.

Don't forget to understand how does this code running. Good luck!

$(document).ready(function() {
    if (location.hash) {
        $("a[href='" + location.hash + "']").tab("show");
    }
    $(document.body).on("click", "a[data-toggle='tab']", function(event) {
        location.hash = this.getAttribute("href");
    });
});
$(window).on("popstate", function() {
    var anchor = location.hash || $("a[data-toggle='tab']").first().attr("href");
    $("a[href='" + anchor + "']").tab("show");
});