Hey there- I need to have a tab interface that has a different "active" tab every time the page loads. There will be advertiser content in each of the 4 tabs, so this is a way to keep things "fair". I am familiar with the tabs part...just need pointed in the right direction getting different "active" tabs to display on page load. Thanks!
A:
Not that familiar with the tabs yet, but you can count items from Javascript and then select one item by random using Javascript's
var randomnumber=Math.floor(Math.random()*tabCount);
and then use the randomnumber as "tab index".
BerggreenDK
2010-03-22 21:20:02
+2
A:
Just use a javascript random object.
var randomnumber=Math.floor(Math.random()*3) //0-3
Then set the id of each tab to be based off of that random number. So if the random number generates a 1, then show tab 2 as the active tab.
Bryan Denny
2010-03-22 21:20:06
Thanks for using my example, you could also have provided your answer as a comment to mine.
BerggreenDK
2010-03-22 21:22:45
@BerggreenDK Pardon? We answered at the same time? I used this as a resource for a quick copy and paste, as it appears you did too: http://www.javascriptkit.com/javatutors/randomnum.shtml
Bryan Denny
2010-03-23 12:57:41
Aahh, well ok. Sorry
BerggreenDK
2010-03-23 14:56:47
thanks...this is exactly what i needed!
tomwolber
2010-03-23 18:08:51
A:
You could let the SERVER which generates the actual content add a "selected default" to the tab in the rendered HTML output.
BerggreenDK
2010-03-22 21:21:30
A:
Using Bryan Denny's answer, here is my almost final code:
// Make sure none of the tabs are active
$('.randomiza-tab1').removeClass('ui-tabs-selected');
$('.randomiza-tab2').removeClass('ui-tabs-selected');
$('.randomiza-tab3').removeClass('ui-tabs-selected');
$('.randomiza-tab4').removeClass('ui-tabs-selected');
// Make sure none of the content is active
$('.randomiza-bucket1').addClass('ui-tabs-hide');
$('.randomiza-bucket2').addClass('ui-tabs-hide');
$('.randomiza-bucket3').addClass('ui-tabs-hide');
$('.randomiza-bucket4').addClass('ui-tabs-hide');
// Find a random number between 0 and 3
var randomnumber=Math.floor(Math.random()*4);
// Add and remove the correct style classes based on random number
switch (randomnumber) {
case 0 :
$('.randomiza-tab1').removeClass('hidden').addClass('ui-tabs-selected');
$('.randomiza-bucket1').removeClass('ui-tabs-hide');
break;
case 1 :
$('.randomiza-tab2').removeClass('hidden').addClass('ui-tabs-selected');
$('.randomiza-bucket2').removeClass('ui-tabs-hide');
break;
case 2 :
$('.randomiza-tab3').removeClass('hidden').addClass('ui-tabs-selected');
$('.randomiza-bucket3').removeClass('ui-tabs-hide');
break;
case 3 :
$('.randomiza-tab4').removeClass('hidden').addClass('ui-tabs-selected');
$('.randomiza-bucket4').removeClass('ui-tabs-hide');
break;
default :
$('.randomiza-tab1').removeClass('hidden').addClass('ui-tabs-selected');
$('.randomiza-bucket1').removeClass('ui-tabs-hide');
}
tomwolber
2010-03-23 18:10:20
@Pafy: if i knew what i was doing, i wouldn't be asking questions here, now would i? thanks for the help.
tomwolber
2010-03-23 20:54:27
Have you tried using this:http://jqueryui.com/demos/tabs/with the `selected` option?
GeReV
2010-03-23 20:57:44
@GeReV: i am using the ui tabs, but i need the "selected" tab to be random, so the advertisers on the different tabs have a "fair" chance of being seen first on page load...hope that makes sense.
tomwolber
2010-03-23 21:03:40
@tomwolber, as Bryan mentioned, get a random number in `var randomnumber`,and initialize the tabs with `$(...).tabs({ selected: randomnumber })`.
GeReV
2010-03-23 22:08:04
+1
A:
GREAT! I got it down to 3 lines! Thanks @Bryan Denny and @GeReV!
JS/jQuery Code:
// Count the number of tabs
var countTabs = $('.tab_randomiza').children().size();
// Find a random number between 0 and the number of tabs (countTabs)
var randomizeIt = Math.floor(Math.random()*(countTabs));
// Tell the tabs which one to be active
$('#randomiza_wrapper').tabs({ selected: randomizeIt });
HTML:
<div class="titlebar titlebar_secondary">
<h3>Test Bucket</h3>
</div><!-- end .titlebar_secondary -->
<div id="randomiza_wrapper">
<ul class="tab_header tab_randomiza">
<li class="randomiza-tab1 ui-tabs-selected"><a href="#one">one</a></li>
<li class="randomiza-tab2"><a href="#two">two</a></li>
<li class="randomiza-tab3"><a href="#three">three</a></li>
<li class="randomiza-tab4"><a href="#four">four</a></li>
</ul>
<div id="one" class="bucket_secondary randomiza_bucket1">
<ul class="story_list sidebar_story_list">
<li>
Content One
</li>
</ul>
</div><!-- end #one -->
<div id="two" class="bucket_secondary preventFOUC randomiza_bucket2">
<ul class="story_list sidebar_story_list ">
<li>
Content Two
</li>
</ul>
</div><!-- end #two -->
<div id="three" class="bucket_secondary preventFOUC randomiza_bucket3">
<ul class="story_list sidebar_story_list ">
<li>
Content Three
</li>
</ul>
</div><!-- end #three -->
<div id="four" class="bucket_secondary preventFOUC randomiza_bucket4">
<ul class="story_list sidebar_story_list ">
<li>
Content Four
</li>
</ul>
</div><!-- end #four -->
</div>
</div><!-- end #top_jobs -->
tomwolber
2010-03-24 13:30:18