Skip to content

Instantly share code, notes, and snippets.

@PHLAK
Created June 16, 2009 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PHLAK/130902 to your computer and use it in GitHub Desktop.
Save PHLAK/130902 to your computer and use it in GitHub Desktop.
Simple AJAX serverd tabs with caching
<h1>My Account</h1>
<div id="account-tabs" class="clearfix">
<ul>
<li id="account-dashboard" class="tabli"><a href="/account/dashboard">Account Dashboard</a></li>
<li id="order-history" class="tabli"><a href="/account/order-history">Order History</a></li>
<li id="address-book" class="tabli"><a href="/account/address-book">Address Book</a></li>
</ul>
</div>
<div id="account-box"></div>
<script type="text/javascript"><!--
$(document).ready(function(){
function createTabs($tabLiClass, $targetContentDivID, $initialTabID){
$("."+$tabLiClass+" a").click(function(){
if($(this).data("loaded") != true){
//make req
var $myEl = this;
$("#"+$targetContentDivID).html('<b>Loading content...</b><br /><br /><img src="/images/loadingAnimation.gif" />');
$.ajax({
url: $(this).attr("href"),
success: function(contents){
$($myEl).data("tabcontents", contents);
$("#"+$targetContentDivID).html($($myEl).data("tabcontents"));
}
});
$(this).data("loaded", true);
} else {
$("#"+$targetContentDivID).html($(this).data("tabcontents"));
}
$("."+$tabLiClass).removeClass('active');
$(this).parent().addClass('active');
return false;
});
$("#"+$initialTabID+" a").click();
}
createTabs("tabli", "account-box","account-dashboard");
});
--></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment