jquery+cookie+json保存浏览商品的记录

 
$(document).ready(function(){
     var p_url = document.URL;
     var p_id=$('#product_id').attr('value');
     var p_img=$('#pro_img').attr('src');
     var p_name=$('#pro_img').attr('alt');
     var old_historyp="";
     var new_historyp='{"id":"'+p_id+'","img":"'+p_img+'","name":"'+p_name+'","url":"'+p_url+'"}';
       var canAdd=true;
     if($.cookie('history')==null){//cookie 不存在     
          new_historyp='(['+new_historyp+'])';
          $.cookie('history',new_historyp,{expires:7,path:'/'});
     }else{//cookies已经存在
          old_historyp=$.cookie('history');
          old_historyp=eval(old_historyp);
          var list = "";
          var i=0;
          $(old_historyp).each(function(){
               if(++i>3){ return false;}//控制显示的数量
               if(this.id!=p_id){//过滤当前的商品
                    list=list + "<li><dl><dt><a href='"+this.url+"' title='"+this.name+"' ><img src='"+this.img+"'></a></dt><dd><a href='"+this.url+"' title='"+this.name+"'>"+this.name+"</a></dd></dl></li>"; 
               }
          })
        // for(var i=0; i<old_historyp.length;i++){
        //     if(old_historyp[i].id!=p_id){
        //    list = list + "<li><dl><dt><a href='"+old_historyp[i].url+"' title='"+old_historyp[i].name+"' ><img src='"+old_historyp[i].img+"'></a></dt><dd><a href='"+old_historyp[i].url+"' title='"+old_historyp[i].name+"'>"+old_historyp[i].name+"</a></dd></dl></li>";
        //     }
        //  }
          $("#historyp").html(list);
          if(canAdd==true){
               $(old_historyp).each(function(){
                    if(this.id!=p_id){ // 判断当前的物品在浏览记录中 如果以前有 通过此 删除以前的记录
                       new_historyp = new_historyp +','+ '{"id":"'+this.id+'","img":"'+this.img+'","name":"'+this.name+'","url":"'+this.url+'"}';
                   }
               })              
             // for(var i=0;i<old_historyp.length;i++){
             //     if(old_historyp[i].id!=p_id){ // 判断当前的物品在浏览记录中 如果以前有 通过此 删除以前的记录
             //         new_historyp = new_historyp +','+ '{"id":"'+old_historyp[i].id+'","img":"'+old_historyp[i].img+'","name":"'+old_historyp[i].name+'","url":"'+old_historyp[i].url+'"}';
             //   }
             // }
               new_historyp='(['+new_historyp+'])';
              $.cookie('history',new_historyp,{expires:7,path:'/'});
          }         
     }
});
</script>











 $(document).ready(function(){
     if($.cookie('history')==null){//cookie 不存在
          $("#historyp_div",this).css("display","none");
     }else{//cookies已经存在
          old_historyp=$.cookie('history');
          old_historyp=eval(old_historyp);
          var list = "";
          var i=0;
          $(old_historyp).each(function(){
               <?php if(isset($app_infos['APP-PHISTORY']['configs']['APP-PHISTORY-NUM'])&&$app_infos['APP-PHISTORY']['configs']['APP-PHISTORY-NUM']!=""&&$app_infos['APP-PHISTORY']['configs']['APP-PHISTORY-NUM']>0){?>
               if(++i><?php echo $app_infos['APP-PHISTORY']['configs']['APP-PHISTORY-NUM'];?>){ return false;}//控制显示的数量
               <?php }?>
                    list=list + "<li><dl><dt><a href='"+this.url+"' title='"+this.name+"' ><img src='"+this.img+"'></a></dt><dd><a href='"+this.url+"' title='"+this.name+"'>"+this.name+"</a></dd></dl></li>"; 
          })
         $("#historyp").html(list);         
     }
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值