Nav-tabs bootstrap setting active on URL

0

I can activate the nav-tab through the url, #Value activates the nav-tab Value. Example:

Or when I click on a tab, the url is populated with #listaReserva. Any suggestions?

    
asked by anonymous 13.07.2016 / 05:22

1 answer

0

You can, for example, access the link :

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<ul class="nav nav-tabs" id="myTab">
    <li class="active"><a data-target="#home" data-toggle="tab">home</a></li>
    <li><a data-target="#listaReservas" data-toggle="tab">Lista de Reservas</a></li>
    <li><a data-target="#lista-espera" data-toggle="tab">Lista de espera</a></li>
</ul>

<div class="tab-content">
    <div class="tab-pane active" id="home">Home</div>
    <div class="tab-pane" id="listaReservas">Lista de Reservas</div>
    <div class="tab-pane" id="lista-espera">Lista de espera</div>
</div>

<script>
    var tab_on = location.hash; // aqui é onde vamos agarrar o valor da hash do url (ex: #listaReservas)
    $('#myTab a[data-target="' +tab_on+ '"]').tab('show');
    $('#myTab a').on('click', function() {
        var this_target = $(this).data('target');
        var window_href = window.location.href.split('#')[0];
        history.pushState('', '', window_href+this_target); // mudar o url dinamicamente
    });
</script>
    
13.07.2016 / 10:52