Link to specific tab Bootstrap

16,360

Solution 1

Following code works for me

HTML

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" />
  <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="col-md-12" style="margin: 0 auto;">
        <section class="panel">
            <header class="panel-heading tab-bg-dark-navy-blue">
                <ul class="nav nav-tabs nav-justified ">
                    <li class="active">
                        <a data-toggle="tab" href="#overview">
                            {% trans "Overview" %}
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#timeline">
                            {% trans "Timeline" %}
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#users_rating">
                            {% trans "Users Ratings" %} ({{ ptc.userrating.count }})
                        </a>
                    </li>
                    <li class="">
                        <a data-toggle="tab" href="#rating">
                            {% trans "Our Rating" %}
                        </a>
                    </li>
                </ul>
            </header>
            <div class="panel-body">
                <div class="tab-content tasi-tab">


                    <!-- Overview Tab-Pane -->
                    <div id="overview" class="tab-pane active">
                        {% include 'overview.html' %}
                    </div>

                    <!-- Timeline Tab-Pane -->

                    <div id="timeline" class="tab-pane">
                        {% include 'timeline.html' %}
                    </div>

                    <!-- User Rating Tab-Pane -->

                    <div id="users_rating" class="tab-pane">
                        {% include 'users_rating.html' %}
                    </div>

                    <!-- Our Rating Tab-Pane -->

                    <div id="rating" class="tab-pane">
                        {% include 'rating.html' %}
                    </div>
                </div>
            </div>

        </section>
    </div>
</body>
</html>

JS

    <script type="text/javascript">
        $(function() {
          // Javascript to enable link to tab
          var hash = document.location.hash;
          if (hash) {
            console.log(hash);
            $('.nav-tabs a[href='+hash+']').tab('show');
          }

          // Change hash for page-reload
          $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
            window.location.hash = e.target.hash;
          });
        });
    </script>

Solution 2

Thanks a bunch. With newer versions of JQuery (mine=3.3.1) you need to make a little alteration:

<script>
     $(function() {
       // Javascript to enable link to tab
       var hash = document.location.hash;
       if (hash) {
         console.log(hash);
         $('.nav-tabs a[href=\\'+hash+']').tab('show');
       }

       // Change hash for page-reload
       $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
         window.location.hash = e.target.hash;
       });
     });
 </script>

Hope this saves someone the hour I lost

Share:
16,360
Alexandre Lara
Author by

Alexandre Lara

Updated on August 07, 2022

Comments

  • Alexandre Lara
    Alexandre Lara almost 2 years

    I'm developing a site with Django Framework and I'm trying to create a way for when a user access a link like http://www.example.com/site/#users_rating it opens a specific tab in the page.

    I tried the following code that I found on the Internet (I'm new in JavaScript/JQuery), it isn't working:

    <script type="text/javascript">
        $(function() {
          // Javascript to enable link to tab
          var url = document.location.toString();
          if (url.match('#')) {
            $('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
          }
    
          // Change hash for page-reload
          $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
            window.location.hash = e.target.hash;
          });
        });
    </script>
    

    My template uses BootStrap 3, here is the HTML code (with some Django tags):

    <div class="col-md-12" style="margin: 0 auto;">
            <section class="panel">
                <header class="panel-heading tab-bg-dark-navy-blue">
                    <ul class="nav nav-tabs nav-justified ">
                        <li class="active">
                            <a data-toggle="tab" href="#overview">
                                {% trans "Overview" %}
                            </a>
                        </li>
                        <li class="">
                            <a data-toggle="tab" href="#timeline">
                                {% trans "Timeline" %}
                            </a>
                        </li>
                        <li class="">
                            <a data-toggle="tab" href="#users_rating">
                                {% trans "Users Ratings" %} ({{ ptc.userrating.count }})
                            </a>
                        </li>
                        <li class="">
                            <a data-toggle="tab" href="#rating">
                                {% trans "Our Rating" %}
                            </a>
                        </li>
                    </ul>
                </header>
                <div class="panel-body">
                    <div class="tab-content tasi-tab">
    
    
                        <!-- Overview Tab-Pane -->
                        <div id="overview" class="tab-pane active">
                            {% include 'overview.html' %}
                        </div>
    
                        <!-- Timeline Tab-Pane -->
    
                        <div id="timeline" class="tab-pane">
                            {% include 'timeline.html' %}
                        </div>
    
                        <!-- User Rating Tab-Pane -->
    
                        <div id="users_rating" class="tab-pane">
                            {% include 'users_rating.html' %}
                        </div>
    
                        <!-- Our Rating Tab-Pane -->
    
                        <div id="rating" class="tab-pane">
                            {% include 'rating.html' %}
                        </div>
    
    
                    </div>
                </div>
    
            </section>
        </div>
    

    How can I open an specific tab according to an URL in my site?