开发者

jQuery - CSS style switcher based on number of visits, using cookies

开发者 https://www.devze.com 2023-03-19 17:43 出处:网络
I am trying to create a jQuery based style switcher which changes the body class depending on the number of visits ot the website.

I am trying to create a jQuery based style switcher which changes the body class depending on the number of visits ot the website.

The problem I am having is that the code I am using changes the class on every page load rather than per visit. The number of visits also alerts a NaN value in Safari.

Do I need to specify a session time? How can this be achieved? I am using the jquery.cookie plugin, here is the code:

if(!$.cookie('visits')){
     $.cookie('visits',1, {expires: 30});
   }
   $.cookie('visits', (parseInt($.cookie('visits')) + 1), {expires: 30}) 
       //will expire after 30 days

       if(parseInt($.cookie('visits')) == 2){
        $('body').removeClass().addC开发者_StackOverflowlass('blue');  
       } else if (parseInt($.cookie('visits')) == 3){
        $('body').removeClass().addClass('green');  
       } else if (parseInt($.cookie('visits')) == 4){
        $('body').removeClass().addClass('purple');  
       } else if (parseInt($.cookie('visits')) == 5){
        $('body').removeClass().addClass('red');  
       } else if (parseInt($.cookie('visits')) > 5){
        $.cookie('visits', 1);               
   };

Help appreciated :)


if(!$.cookie('visits')){
     $.cookie('visits',0, {expires: 30});
   }
if(!$.cookie('last') or parseInt($.cookie('last'))+600000<new Date().getTime()) // 10minutes expired, new visit

$.cookie('visits', (parseInt($.cookie('visits')) + 1), {expires: 30}) 
   //will expire after 30 days
   $.cookie('last',new Date().getTime(),{expires: 1});
   if(parseInt($.cookie('visits')) == 2){
    $('body').removeClass().addClass('blue');  
   } else if (parseInt($.cookie('visits')) == 3){
    $('body').removeClass().addClass('green');  
   } else if (parseInt($.cookie('visits')) == 4){
    $('body').removeClass().addClass('purple');  
   } else if (parseInt($.cookie('visits')) == 5){
    $('body').removeClass().addClass('red');  
   } else if (parseInt($.cookie('visits')) > 5){
    $.cookie('visits', 1);               
};
0

精彩评论

暂无评论...
验证码 换一张
取 消