Trying to randomize stylesheet if a cookie has not been set
I've got a web site that allows the user to switch between two different CSS looks for the site via jQuery. Once a user selects one, it sets a 365 day cookie to remember which style sheet the user selected. I'm trying to figure out a way that if a cookie isn't set, to randomize between the two style sheets.
Here's the relevant jQuery (core and cookie plugin are loaded higher in the .js file). I'm think I need to add some sort of random "grab" in the else.
if($.cookie("css")) {
$("link").attr("href",$.cookie("css"));
}else{
}
$(function() {
$("#designSwap li a").click(function() {
$("link").attr("href",$(this).attr('rel'));
$.cookie("cs开发者_运维百科s",$(this).attr('rel'), {expires: 365, path: '/'});
var themeColor = 'dark';
updateColor($(this).attr('class'));
return false;
});
});
Here's the HTML:
<ul id="designSwap">
<li><a class="dark" href="#" title="Alternate Design #1" rel="/lib/css/common-dark.css"></a></li>
<li><a class="light" href="#" title="Default Design" rel="/lib/css/common.css"></a></li>
<li>Choose your scheme:</li>
</ul>
Edit: I've got this up, running and functioning here: http://centerline.net, if anyone wants to see the swapping of style sheets and cookie setting in action.
var rand = Math.floor(Math.random()*2)
if (rand > 0)
$("link").attr("href","/lib/css/common-dark.css");
else
$("link").attr("href","/lib/css/common.css");
精彩评论