Keep the Current Tab Active on Page Reload - HTML CSS Bootstrap

HTML CSS examples for Bootstrap:Tab

Description

Keep the Current Tab Active on Page Reload

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
 <head> 
  <meta charset="UTF-8"> 
  <title>Keep Last Selected Bootstrap Tab Active on Page Refresh</title> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
  <script type="text/javascript">
$(document).ready(function(){
  $('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
    localStorage.setItem('activeTab', $(e.target).attr('href'));
  });<!--from  ww w.  j av a2  s. com-->
  var activeTab = localStorage.getItem('activeTab');
  if(activeTab){
    $('#myTab a[href="' + activeTab + '"]').tab('show');
  }
});
</script> 
  
 </head> 
 <body> 
  <div> 
   <ul class="nav nav-tabs" id="myTab"> 
    <li class="active"><a data-toggle="tab" href="#sectionA">Section A</a></li> 
    <li><a data-toggle="tab" href="#sectionB">Section B</a></li> 
    <li><a data-toggle="tab" href="#sectionC">Section C</a></li> 
   </ul> 
   <div class="tab-content"> 
    <div id="sectionA" class="tab-pane fade in active"> 
     <h3>Section A</h3> 
     <p>Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui. Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth.</p> 
    </div> 
    <div id="sectionB" class="tab-pane fade"> 
     <h3>Section B</h3> 
     <p>Vestibulum nec erat eu nulla rhoncus fringilla ut non neque. Vivamus nibh urna, ornare id gravida ut, mollis a magna. Aliquam porttitor condimentum nisi, eu viverra ipsum porta ut. Nam hendrerit bibendum turpis, sed molestie mi fermentum id. Aenean volutpat velit sem. Sed consequat ante in rutrum convallis. Nunc facilisis leo at faucibus adipiscing.</p> 
    </div> 
    <div id="sectionC" class="tab-pane fade"> 
     <h3>Section C</h3> 
     <p>Vestibulum nec erat eu nulla rhoncus fringilla ut non neque. Vivamus nibh urna, ornare id gravida ut, mollis a magna. Aliquam porttitor condimentum nisi, eu viverra ipsum porta ut. Nam hendrerit bibendum turpis, sed molestie mi fermentum id. Aenean volutpat velit sem. Sed consequat ante in rutrum convallis. Nunc facilisis leo at faucibus adipiscing.</p> 
    </div> 
   </div> 
  </div>   
 </body>
</html>

Related Tutorials