Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep the current tab active with twitter bootstrap after a page reload?

I know this question was asked before and answered in the following post:

How do I keep the current tab active with twitter bootstrap after a page reload?

However, given my limited knowledge of scripting, I have not been able to get this to work on my website. After putting the code in my html page the tabs still go to the first and active one on page reload.

Please can someone share a working example of the solution presented in the link above.

My code looks like this:

<div class="tabbable" style="margin-bottom: 8px;">
        <ul class="nav nav-tabs">
          <li class="active"><a href="#tab1" data-toggle="tab">My Career Centre</a></li>
          <li class=""><a href="#tab2" data-toggle="tab">Hiring Companies</a></li>
          <li class=""><a href="#tab3" data-toggle="tab">Training Providers</a></li>
          <li class=""><a href="#tab4" data-toggle="tab">Advertise</a></li>
          <li class=""><a href="#tab5" data-toggle="tab">Graduate Opportunities</a></li>
          <li class=""><a href="#tab6" data-toggle="tab">Career Forum</a></li>
        </ul>           
        <script type="text/javascript">
                  $(function() 
                    { 
                      $('a[data-toggle="tab"]').on('shown', function (e) {
                        //save the latest tab; use cookies if you like 'em better:
                        localStorage.setItem('lastTab', $(e.target).attr('id'));
                      });

                      //go to the latest tab, if it exists:
                      var lastTab = localStorage.getItem('lastTab');
                      if (lastTab) {
                          $('#'+lastTab).tab('show');
                      }
                    });
            </script>

        <div class="tab-content" style="padding: 0 5px;">
           <div class="tab-pane active" id="tab1">
                <div class="navbar" style="width:930px;">
                  <div class="navbar-inner">
                    <div class="container">
                      <ul class="nav2" style="padding-top:0px;">
                        <li><a href="#">Job Search</a></li>
                        <li><a href="#">Training Programmes</a></li>
                        <li><a href="#">Job Alerts</a></li>
                        <li><a href="#">My Job Applications</a></li>
                        <li><a href="#">My Studies</a></li>
                        <li><a href="http://localhost:8080/nationalcareers/JOBSEEKERS/index.php?category=home&action=received">Received Messages</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
           </div>
           <div class="tab-pane" id="tab2">
            <div class="navbar" style="width:900px;">
                  <div class="navbar-inner">
                    <div class="container">
                      <ul class="nav2" style="padding-top:0px;">
                        <li><a href="#">Manage Your Job Adverts</a></li>
                        <li><a href="#">Browse CV's</a></li>
                        <li><a href="#">Manage Job Applications</a></li>
                        <li><a href="http://localhost:8080/nationalcareers/EMPLOYERS/index.php?category=postings&action=my">View Reports For Your Adverts</a></li>
                        <li><a href="http://localhost:8080/nationalcareers/EMPLOYERS/index.php?category=home&action=sub_accounts">Manage Sub-Accounts</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
          </div>
          <div class="tab-pane" id="tab3">
            <div class="navbar" style="width:900px;">
                  <div class="navbar-inner">
                    <div class="container">
                      <ul class="nav2" style="padding-top:0px;">
                        <li><a href="#">Browse Training Programmes</a></li>
                        <li><a href="#">Browse Featured Training Providers</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
           </div>
           <div class="tab-pane" id="tab4">
            <div class="navbar" style="width:900px;">
                  <div class="navbar-inner">
                    <div class="container">
                      <ul class="nav2" style="padding-top:0px;">
                        <li><a href="#">Jobs</a></li>
                        <li><a href="#">Training Programmes</a></li>
                        <li><a href="#">Your Company</a></li>
                        <li><a href="#">Your Recruitment Agency</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
           </div>
           <div class="tab-pane" id="tab5">
            <div class="navbar" style="width:900px;">
                  <div class="navbar-inner">
                    <div class="container">
                      <ul class="nav2" style="padding-top:0px;">
                        <li><a href="#">Search Current Opportunities</a></li>
                        <li><a href="#">News and Updates</a></li>
                        <li><a href="#">Events</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
          </div>
          <div class="tab-pane" id="tab6">
            <div class="navbar" style="width:900px;">
                  <div class="navbar-inner">
                    <div class="container">
                      <ul class="nav2" style="padding-top:0px;">
                        <li><a href="#">Career Advice</a></li>
                        <li><a href="#">CV/Resume</a></li>
                        <li><a href="#">Interview Preparation</a></li>
                        <li><a href="#">Career Net-Work</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
           </div>
        </div>
        </div>
</div>

Highly appreciated,

like image 592
Grand Masta D Avatar asked May 29 '13 07:05

Grand Masta D


People also ask

How do I stay on the tab after refresh?

Here is a simple JS code to display active tab when page is loaded. In the above code, we set the item activeTab in local storage to the href attribute of active tab, whenever it is changed, that is, whenever a tab is displayed. This gets triggered every time the user changes tabs.

How do I keep the button active after page refresh?

You can save the state of the button throughout page reloads using sessionStorage or localStorage . sessionStorage: The sessionStorage property accesses a session Storage object for the current origin.

How do I make bootstrap tab active?

To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a .tab-pane class with a unique ID for every tab and wrap them inside a <div> element with class .tab-content .

How do you make a tab active by default?

Just add the class to your html. This will make the first tab active by default.


2 Answers

As Tommi Komulainen wrote: e.target contains the full url including the hash. You only need the hash. So use e.target.toString().split('#')[1]); or even better $(this).attr('href') $('#'+lastTab).tab('show'); applies the .tab() on the div with id = #{lastTab} while you need to apply on the link (a tag) with data-toggle. So use: $('a[href=#' + lastTab + ']').tab('show'); here.

The complete function to use:

                  $(function() 
                    { 
                      $('a[data-toggle="tab"]').on('shown', function () {
                        //save the latest tab; use cookies if you like 'em better:
                        localStorage.setItem('lastTab', $(this).attr('href'));
                       });

                      //go to the latest tab, if it exists:
                      var lastTab = localStorage.getItem('lastTab');
                      if (lastTab) {
                         $('a[href=' + lastTab + ']').tab('show');
                      }
                      else
                      {
                        // Set the first tab if cookie do not exist
                        $('a[data-toggle="tab"]:first').tab('show');
                      }
                  });

update: see https://stackoverflow.com/a/16016592/1596547 so remove the active class from your source and set the first tab active when lastTab is not set.

like image 91
Bass Jobsen Avatar answered Oct 11 '22 03:10

Bass Jobsen


I prefer to use a pushstate.

It seems more like "The web way..." to me and it allows me to have external links pointing right to the tab I want to show.

This is my function:

function setBootstrapTabs()
{
    var activeTab = "#" + getParameterByName("submenu");

    $('a[data-toggle="tab"]').on('shown', function () {
        //save the latest tab;
        var new_url = updateUrlParam("submenu",
            $(this).attr('href').replace("#",""));
        window.history.replaceState({
                turbolinks: true, position: Date.now()
            }, document.title, new_url
        );
    });

    if (activeTab.length > 0) {
        $('a[href=' + activeTab + ']').tab('show');
    }
};
like image 2
ndemoreau Avatar answered Oct 11 '22 01:10

ndemoreau