A solution can go through the use of property cookie
:
Get and set the cookies associated with the current document.
Specifications can be read in: DOM Level 2: Htmldocument.cookie
Compatibility
The solution below only needs basic support (read, write), and in terms of compatibility we can observe that the solution works virtually in any browser:
┌────────────────────────────────────────────────────────────────────────┐
│ Suporte em Desktop │
├────────┬─────────────────┬───────────────────┬───────┬─────────────────┤
│ Chrome │ Firefox (Gecko) │ Internet Explorer │ Opera │ Safari (WebKit) │
├────────┼─────────────────┼───────────────────┼───────┼─────────────────┤
│ Sim │ Sim │ Sim │ Sim │ Sim │
└────────┴─────────────────┴───────────────────┴───────┴─────────────────┘
┌────────────────────────────────────────────────────────────────────────────┐
│ Suporte em Mobile │
├─────────┬────────────────────────┬──────────┬──────────────┬───────────────┤
│ Android │ Firefox Mobile (Gecko) │ IE Phone │ Opera Mobile │ Safari Mobile │
├─────────┼────────────────────────┼──────────┼──────────────┼───────────────┤
│ Sim │ Sim │ Sim │ Sim │ Sim │
└─────────┴────────────────────────┴──────────┴──────────────┴───────────────┘
Code
The relevant part is in Javascript, where we will read and write the cookie so that we can load the correct tab when the visitor returns:
/* document.cookie devolve todos os cookies acessíveis pelo presente documento.
* Tão cedo quanto possível, realizamos uma filtragem para ficar apenas
* com o valor do cookie que nos interessa.
*/
$(function(){
var ultimaTab = document.cookie.replace(/(?:(?:^|.*;\s*)tabAtiva\s*\=\s*([^;]*).*$)|^.*$/, "$1");
$('a[href="'+ultimaTab+'"').trigger("click");
});
/* Twitter Bootstrap tem eventos associados às tabs, pelo que podemos fazer uso
* dos mesmos para que ao ser apresentada uma nova tab, estejamos também a guardar
* a sua identificação no nosso cookie.
*/
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var tab = $(this).attr("href");
document.cookie = "tabAtiva="+tab+"; expires=Fri, 28 Dec 2040 23:59:59 GMT; path=/";
});
Example
This example is also available at Jsfiddle where we can access it, change tab, close the browser tab, go back to Jsfiddle and check that the tab opened at the time we close the tab is effectively the one opened when we return to Jsfiddle.
$(function(){
var ultimaTab = document.cookie.replace(/(?:(?:^|.*;\s*)tabAtiva\s*\=\s*([^;]*).*$)|^.*$/, "$1");
$('a[href="'+ultimaTab+'"').trigger("click");
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var tab = $(this).attr("href");
document.cookie = "tabAtiva="+tab+"; expires=Fri, 28 Dec 2040 23:59:59 GMT; path=/";
});
body{
padding:10px;
font-size:9px;
}
.tab-content{
padding-top:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<ul class="nav nav-tabs" role="tablist" id="myTab">
<li role="presentation" class="active">
<a href="#novas" aria-controls="novas" role="tab" data-toggle="tab">Novas Mensagens</a>
</li>
<li role="presentation">
<a href="#andamento" aria-controls="andamento" role="tab" data-toggle="tab">Em Andamento</a>
</li>
<li role="presentation">
<a href="#resolvidas" aria-controls="resolvidas" role="tab" data-toggle="tab">Resolvidas</a>
</li>
<li role="presentation">
<a href="#canceladas" aria-controls="canceladas" role="tab" data-toggle="tab">Canceladas</a>
</li>
<li role="presentation">
<a href="#pendentes" aria-controls="pendents" role="tab" data-toggle="tab">Pendentes</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="novas">
A minha tab com as novas entradas
</div>
<div role="tabpanel" class="tab-pane" id="andamento">
Assuntos em andamento
</div>
<div role="tabpanel" class="tab-pane" id="resolvidas">
Cenas resolvidas
</div>
<div role="tabpanel" class="tab-pane" id="pendentes">
Cenas pendentes
</div>
<div role="tabpanel" class="tab-pane" id="canceladas">
Cenas canceladas
</div>
</div>
In the example using the snippet of SE, the example fails due to the way it is loaded.
If it’s not single page you will need to use some way to store the latter tab clicked on the client. For example, with
localStorage
orsessionStorage
.– Renan Gomes
how so single page? I think I’m misinterpreting your answer kkk
– Saymon