Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Back Button Handling With Twitter Bootstrap Tabs in IE8+

Bootstrap 2.3.2

I am using tabs from Bootstrap in my project to display content. On a single page there are multiple tabs with different content and in those tabs are links to take the user to other pages on the website.

Unfortunately by default, the tabs don't support back button navigation, so if a user visits another page and hits the browser's back button, the tab will have reverted to the first open tab rather than the one that was active when they last viewed the page.

The Javascript below from François Zaninotto solves this problem in Chrome, Firefox and Safari. François also says it works for IE8+ - but during my tests I can't get it to work on IE8, IE9 or IE10. Does anyone know how to get this working in IE8+ ? Is it possible?

$(document).ready(function() {
  // add a hash to the URL when the user clicks on a tab
  $('a[data-toggle="tab"]').on('click', function(e) {
    history.pushState(null, null, $(this).attr('href'));
  });
  // navigate to a tab when the history changes
  window.addEventListener("popstate", function(e) {
    var activeTab = $('[href=' + location.hash + ']');
    if (activeTab.length) {
      activeTab.tab('show');
    } else {
      $('.nav-pills a:first').tab('show');
    }
  });
});

`

    <ul id="myTab" class="nav nav-pills">
              <li class="active"><a href="#tab1" data-toggle="tab">Tab1</a></li>
              <li><a href="#tab2" data-toggle="tab">Tab2</a></li>
        <li><a href="#tab3" data-toggle="tab">Tab3</a></li>

            </ul>

<div id="myTabContent" class="tab-content">
              <div class="tab-pane active" id="tab1">
                  <h2>Tab 1</h2>
                <p>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. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
              </div>
              <div class="tab-pane" id="tab2">
                  <h2>Tab 2</h2>
                <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p>
              </div>
              <div class="tab-pane" id="tab3">
                    <h2>Tab 3</h2>
                <p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p>
              </div>
            </div>
like image 677
Metzed Avatar asked Nov 03 '22 17:11

Metzed


1 Answers

My implementation is probably not the most efficient way of solving your problem so please wait for other answers.

Persist javascript variables across pages?

Use JavaScript sessions to store unique number of the tab, as you currently use for the ID.

tabHistory = "tab" + currentTab;

On return to previous page, simply link the user to tabHistory.

I don't have much experience with this technique, only having used it once, however it should be quick and simple to implement, also quite efficient.

From what I have gathered, it will not work if a new window/tab is open, but neither will the back button so it's irrelevant.

like image 133
Jakub Wawszczyk Avatar answered Nov 07 '22 21:11

Jakub Wawszczyk