I have a basic jQuery tabs system going:
$(document).ready(function(){
$('#tabs div.jdiv').hide();
$('#tabs div.jdiv:first').fadeIn("slow");
$('#tabs ul.jdiv li:first').addClass('active');
$('#tabs ul.jdiv li a').click(function(){
$('#tabs ul.jdiv li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div.jdiv').hide();
$(currentTab).fadeIn("slow");
return false;
});
});
<div id="tabs" style="position:relative; ">
<ul class="jdiv">
<li><a href="#current-points">Current Points</a></li>
<li><a href="#my-details">My Details</a></li>
<li><a href="#prizes">Prizes</a></li>
<li><a href="#basket">Your sack</a></li>
<li><a href="#order-history">Order History</a></li>
</ul>
<div id="current-points" class="jdiv" style="position:relative;">
<?php include('current-points.php');?>
</div>
etc….
My issue is, I am planning on having another set of jQuery tabs within one of these pages, which isnt a problem, but when they click on a link or refresh the page, is it possible to stay on the same tab? On the parent or child set of tabs?
Thanks!
You can use hash tags to uniquely identify each tab, so
http://example.com/yourpage.html#current-pointstakes you to thecurrent-pointstab, andhttp://example.com/yourpage.html#my-detailstakes you to themy-detailstab. You can set the hash by assigning tolocation.hash, and of course you can read that on page load. This also has the huge advantage that your users can bookmark the tabs they want. You can use a path in the hash if you have tabs within tabs (so#first/footakes you to thefirsttab and itsfoosubtab;#first/bartakes you to thefirsttab and itsbarsubtab).Here’s a really basic example (without subtabs, but you get the idea):
Live copy | Live source
HTML:
JavaScript:
Alternately (or in conjunction), you can set a cookie when they change tabs, and check for the cookie on page load to select the last tab they had selected.