I'm adapting a site with several content heavy tabs so that they lazily load when or if they're first clicked. To do this, I've written the following jquery:
$(document).ready(function(){
$("#aa,#bb,#cc,#dd,#ee").bind("click", function() {
target = $(this).data('target');
section = $(this).data('section');
if ($("#"+target).html() == "Loading") {
urlstring = "mysite.com/item-tab.html?item=666&tabType="+section;
$.ajax({ url: urlstring }).done(
function(msg){
$("#"+target).html(msg)
});
}
});
});
in the HTML I have:
<ul class="nav nav-pills nav-justified hidden-print">
<li class="first-tab active" id="aa" data-target="specContent" data-section="spec"><a href="#a" data-toggle="pill">Specification</a></li>
<li id="bb" data-target="vidContent" data-section="videos"><a href="#b" data-toggle="pill">Videos</a></li>
<!-- a bunch more like this -->
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" id="a">
<h2>Product Specification</h2>
<div id="specContent">Loading</div>
</div>
<div class="tab-pane fade" id="b">
<h2>Product Videos</h2>
<div id="vidContent">Loading</div>
</div>
<!-- a bunch more of these too -->
</div>
Now I've hit a wall. Normally the first tag will be open on page load but occasionally it'll be the second, done through the URL having #bb at the end.
How can I pick up on this and load the second tab's content at the start? Is there an event triggered that I can check for when the second tab is activated through #bb in the URL?
My other possible direction was to access the URL and split on # to extract bb, loading the second tab if I found it but searching SO on this seemed to suggest I'd be opening my script up to potential security risks by doing that.
I should add that if it was always the first tab open at the start, I wouldn't have included #aa in the jquery and would have put the real content, not "Loading" into the div id="specContent"
Thanks for any recommendations, suggestions or pointers.