Stay on same tab after Refresh

4

I'm developing a website in jQuery. And so far I've created a simple menu with content appearing inside divs.

Menu

<ul class="menu" id="menu">
    <li class="menu-text"><h4>Titulo</h4></li> 
    <li class="current"><a href="#1" data-id="div1">Menu1</a></li>
    <li><a href="#2" data-id="div2">Menu2</a></li>
    <li><a href="#3" data-id="div3">Menu3</a></li>
</ul>

Content in Div's

<div class="pbox" id="div1"> ... </div>
<div class="pbox" id="div2"> ... </div>
<div class="pbox" id="div3"> ... </div>

jQuery

$('#menu').on('click', 'a', function () {
    if(!($(this).closest('li').hasClass("current"))){
        $('.current').not($(this).closest('li').addClass('current')).removeClass('current');
        // fade out all open subcontents
        $('.pbox:visible').hide(600);
        // fade in new selected subcontent
        $('.pbox[id=' + $(this).attr('data-id') + ']').show(600);
    }
});

The only problem with using tabs is that refreshing the page automatically returns to the first tab, in this case # 1 .

Is there any way to refresh, stay on the same tab?

    
asked by anonymous 29.05.2017 / 19:04

3 answers

2

Resolution:

After looking for help in the English stackoverflow, I was able to solve my problem. The final code looks like this:

jQuery

Select tab:

var menu = $('#menu');
    menu.on('click', 'a', function () {
    var current = $(this).closest('li');
    if(!current.hasClass("current")){        
    $('.current').removeClass('current');
    current.addClass("current");

    var id = $(this).attr('data-id');
    $('.pbox:visible').hide(600);
    $('#' + id + '.pbox').show(600);
    history.pushState(null, null, '#' + id);
  }
});

Continue on same tab after refresh:

var menuId = location.hash.substring(1);
$( document ).ready(function() {
if (menuId) {
    var menu = $('#menu');
    var current = $("li[id='" + menuId + "']");
    if(!current.hasClass("current")){
    $('.current').removeClass('current');
    current.addClass("current");
    $('.pbox:visible').hide();
    $('#' + menuId + '.pbox').show(600);
    history.pushState(null, null, '#' + menuId);
}
}
});
    
31.05.2017 / 17:46
1

update the hash of the page when changing the tab, then when loading the page check the hash.

var menu = $('#menu');
menu.on('click', 'a', function () {
  var current = $(this).closest('li');
  if(!current.hasClass("current")){        
    $('.current').removeClass('current');
    current.addClass("current");
		
    var id = $(this).attr('data-id');		
    $('.pbox:visible').hide(600);
    $('#' + id + '.pbox').show(600);
    history.pushState(null, null, '#' + id);
  }
});

var menuId = location.hash.substring(1);
if (menuId) {
  $(menu, "li a[data-id='" + menuId + "]").trigger('click');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ulclass="menu" id="menu">
  <li class="menu-text"><h4>Titulo</h4></li> 
  <li class="current"><a href="#1" data-id="div1">Menu1</a></li>
  <li><a href="#2" data-id="div2">Menu2</a></li>
  <li><a href="#3" data-id="div3">Menu3</a></li>
</ul>

<div class="pbox" id="div1"> Conteudo 1 </div>
<div class="pbox" id="div2"> Conteudo 2 </div>
<div class="pbox" id="div3"> Conteudo 3 </div>
    
29.05.2017 / 20:37
0

Using the bootstrap I did it this way:

$('ul.nav-tabs li a').click(function(e){
    e.preventDefault();
    $(this).tab('show');
    window.scrollTo(0, 0);
});

$('ul.nav-tabs > li > a').on('shown.bs.tab', function(e){
    var id = $(e.target).attr("href").substr(1);
    window.location.hash = id;
});

var hash = window.location.hash;
$('ul.nav-tabs li a[href="' + hash + '"]').tab('show');
    
29.05.2017 / 19:36