Javascript操纵Cookie实现购物车程序

复制代码 代码如下:
/*****************************************************************************************************
Name    购物车
Version    1.1
Author    Vanni(凡林)url:www.27sea.comQQ:303590170
CreateDate  2005-05-31
Description
  此类是基于JavaScript和客户端Cookie,请保证客户端开启Cookie
  数据保持(默认24*30小时)可以通过this.expire=?小时来指定
  类中两自带的两个对象typeObj和proObj均有两个相同属性名:name和value

类中数据存储形式如下-----------------------------------
Array(
newtypeObj('汽车',array(
    newporObj('三菱',200),
    newproObj('本田',500)
)
  ),
  newtypeObj('蛋',array(
    newproObj('鸡蛋',10),
    newproObj('鸭蛋',20)
  )
}

Cookie存取形式为[使用escape()函数加密过]--------------
  购物车名=汽车#三菱:200|本田:500,蛋#鸡蛋:10|鸭蛋:20

注意:客户端存Cookie时,不会出现问题。如果要循环存储的话,可能会出现有些存入,而有些未存入
   解决方法:见下例(获得URL里的sales的数量,并存入Cookie)

文件:/depot/compareproduct.php中的JS代码片段
<scriptlanguage="javascript">
varcar=newCar('compare');
vartypeName='list';
car.delType(typeName);    //将先前对比的产品清除

//得到URL里的参数,并分隔成数组
varurl=location.href;
varstart=url.lastIndexOf('?sales=');
varend=url.indexOf('&');
if(end==-1)end=url.length;
varurlparam=url.substring(url.lastIndexOf('?sales=')+7,end).split(',');

functionsetPageVal(){
  if(car.getPro(typeName).length==urlparam.length)return;    //关键部分,如果数组长度不相等说明,有些Cookie没有存入
  else{
    car.addType(typeName);            //增一个类别
    for(i=0;i<urlparam.length;i++){
      car.addPro(typeName,urlparam[i],'');  //增加对比产品,如果存在,返回假
    }
    setTimeout('setPageVal();',100);      //再次调用自身,没有用递归,是因为递归速度太快,仍会有存不进的问题
  }
}

setPageVal();                    //初始化数据

functiondelItem(itemname){
  car.delPro(typeName,itemname);
  varcarData=car.getPro(typeName);
  varurl='';
  varcarlen=carData.length;
  if(carlen>1){
    for(i=0;i<carData.length;i++){
      if(i==0)  url=carData[i].name;
      else    url+=','+carData[i].name;
    }
    document.write("waiting....");
    location.href='../depot/compareproduct.php?sales='+url;
  }else{
    if(confirm('如果删除它,那么只剩一个对比项了,是否关闭此窗口?')){
      car.delCar();
      window.close();
    }
  }
}
</script>

*****************************************************************************************************/
/**
Cookie类
*/
functionCookie(){
  /**
  @desc设置Cookie
  @returnvoid
  */
  this.setCookie=function(name,value,hours){
    varexpire="";
    if(hours!=null){
      expire=newDate((newDate()).getTime()+hours*3600000);
      expire=";expires="+expire.toGMTString();
    }
    document.cookie=escape(name)+"="+escape(value)+expire;
  }
  
  /**
  @desc读取Cookie
  @returnString
  */
  this.getCookie=function(name){
    varcookieValue="";
    varsearch=escape(name)+"=";
    if(document.cookie.length>0){
      offset=document.cookie.indexOf(search);
      if(offset!=-1){
        offset+=search.length;
        end=document.cookie.indexOf(";",offset);
        if(end==-1)end=document.cookie.length;
        cookieValue=unescape(document.cookie.substring(offset,end))
      }
    }
    returncookieValue;    
  }  
}

functionCar(name){
  
  if(!window.clientInformation.cookieEnabled){
    alert('你的浏览器不支持Cookie无法使用此购物车系统');
    returnfalse;
  }
  
  //##内部变量#############################################################
  
  this.carName=name;
  this.expire  =24*30;    //购物车的有效时间(30天)
  this.carDatas=newArray();
  this.cookie  =newCookie();
  
  //##内部对象#############################################################
  
  this.typeObj=function(name,value){  //自带的类别对象
    this.name=name;
    this.value="/value;
  }
  this.proObj=function(name,value){  //自带的"商品对象
    this.name=name;
    this.value=value;
  }
  
  //##私有方法列表##########################################################
  //
  //  getTypePoint(typeName);        //得到购物车里类别数组里的下标
  //  getProPoint(typeName,proName);    //得到购物车里类别下的产品下标
  //  saveCookie()            //以特定的形式存储此购物车的Cookie
  //
  //########################################################################
  
  /**
  @desc得到购物车里类别数组里的下标,找到的话返回下标,否则返回-1
  @returnint
  */
  this.getTypePoint=function(typeName){
    varisok=false;
    vari=0;
    for(;i<this.carDatas.length;i++){
      if(this.carDatas[i].name==typeName){
        isok=true;      //找到位置
        break;
      }
    }
    if(isok)  returni;
    else    return-1;
  }
  
  /**
  @desc得到购物车里类别下的产品下标,找到返回下标,否则返回-1
  @returnint
  */
  this.getProPoint=function(typeId,proName){
    varisok=false;
    varj=0;
    vartempProObj=this.carDatas[typeId].value;
    for(;j<tempProObj.length;j++){
      if(tempProObj[j].name==proName){
        isok=true;
        break;  
      }
    }
    if(isok)  returnj;
    else    return-1;
  }
  
  /**
  @desc存储生成的Cookie字符串
  @returnvoid
  */
  this.saveCookie=function(){
    varoutStr='';
    for(i=0;i<this.carDatas.length;i++){
      vartypeName=this.carDatas[i].name;
      vartypeValue=this.carDatas[i].value;
      varproOutStr='';
      for(j=0;j<typeValue.length;j++){
        if(j==0)  proOutStr=typeValue[j].name+':'+typeValue[j].value;
        else    proOutStr+='|'+typeValue[j].name+':'+typeValue[j].value;
      }
      if(i==0)  outStr=typeName+'#'+proOutStr;
      else    outStr+=','+typeName+'#'+proOutStr;
    }
    this.cookie.setCookie(this.carName,outStr,this.expire);  //存入Cookie  
  }
    
  //##构造语句############################################################
  
  if(this.cookie.getCookie(name)==''){
    this.cookie.setCookie(name,'',this.expire);
  }else{
    vartempTypes=this.cookie.getCookie(name).split(',');
    for(i=0;i<tempTypes.length;i++){
      vartempTypeObj=tempTypes[i].split('#');
      vartype_pro=newArray();
      if(tempTypeObj[1]){
        vartempProObj=tempTypeObj[1].split('|');
        for(j=0;j<tempProObj.length;j++){
          varproDesc=tempProObj[j].split(':');
          type_pro.push(newthis.proObj(proDesc[0],proDesc[1]));
        }
      }
      this.carDatas.push(newthis.typeObj(tempTypeObj[0],type_pro));
    }
  }

  //##公共方法列表#########################################################
  //
  //  addType(typeName);          //增加一个类别
  //  addPro(typeName,proName,value);    //增加一个产品
  //  editPro(typeName,proName,value);  //修改产品的值
  //  delPro(typeName,proName);      //删除购物车内的一个类别下的产品
  //  delType(typeName);          //删除购物车内的一个类别,包括类别下的产品
  //  delCar();              //删除购物车
  //  
  //  getCar();              //得到整个购物车的数据
  //  getType();              //得到购物车内的所有类别列表
  //  getPro(typeName);          //得到购物车内指定类别下的产品列表
  //  getProVal(typeName,proName);    //得到购物车内指定类别下的产品属性
  //
  //########################################################################
  
  /**
  @desc在购物车里增加一个类别,增加成功返回真,否则返回假
  @returnbool
  */
  this.addType=function(typeName){
    if(this.getTypePoint(typeName)!=-1)    returnfalse;        //如果已经有此类别了,返回假
    this.carDatas.push(newthis.typeObj(typeName,newArray()));      //push进自身数组
    this.saveCookie();  //存入Cookie
    returntrue;
  }
  
  /**
  @desc在购物车里增加一个产品,增加成功返回真,否则返回假
  @returnbool
  */
  this.addPro=function(typeName,proName,value){
    vartypePoint=this.getTypePoint(typeName);      if(typePoint==-1)returnfalse;    //没有此类别,无法增加,返回假
    varproPoint=this.getProPoint(typePoint,proName);  if(proPoint!=-1)returnfalse;    //有此产品了,无法增加重复,返回假
    this.carDatas[typePoint].value.push(newthis.proObj(proName,value));  //push到自身数组
    this.saveCookie();  //存入Cookie
    returntrue;
  }
  
  /**
  @desc修改购物车里的产品属性
  @returnbool
  */
  this.editPro=function(typeName,proName,value){
    vartypePoint=this.getTypePoint(typeName);      if(typePoint==-1)returnfalse;  //没有此类别,无法修改,返回假
    varproPoint=this.getProPoint(typePoint,proName);  if(proPoint==-1)returnfalse;  //没有此产品,无法修改,返回假
    this.carDatas[typePoint].value[proPoint].value=value;              //更新自身
    this.saveCookie();  //存入Cookie
    returntrue;
  }
  
  /**
  @desc删除一个产品
  @returnbool
  */
  this.delPro=function(typeName,proName){
    vartypePoint=this.getTypePoint(typeName);      if(typePoint==-1)returnfalse;  //没有此类别,无法删除,返回假
    varproPoint=this.getProPoint(typePoint,proName);  if(proPoint==-1)returnfalse;  //没有此产品,无法删除,返回假
    varpros=this.carDatas[typePoint].value.length;
    this.carDatas[typePoint].value[proPoint]=this.carDatas[typePoint].value[pros-1];  //最后一个产品放置要删除的产品上
    this.carDatas[typePoint].value.pop();
    this.saveCookie();  //存入Cookie
    returntrue;
  }
  
  /**
  @desc删除一个类别
  @returnbool
  */
  this.delType=function(typeName){
    vartypePoint=this.getTypePoint(typeName);  if(typePoint==-1)returnfalse;  //没有此类别,无法删除,返回假
    vartypes=this.carDatas.length;
    this.carDatas[typePoint]=this.carDatas[types-1];            //删除类别
    this.carDatas.pop();
    this.saveCookie();  //存入Cookie
    returntrue;
  }
  
  /**
  @desc删除此购物车
  @returnvoid
  */
  this.delCar=function(){
    this.cookie.setCookie(this.carName,'',0);
    this.carDatas=newArray();
    this.saveCookie();  //存入Cookie
  }
  
  /**
  @desc获得购物车数据
  @returnArray
  */
  this.getCar=function(){
    returnthis.carDatas;
  }
  
  /**
  @desc获得类别列表
  @returnArray
  */
  this.getType=function(){
    varreturnarr=newArray();
    for(i=0;i<this.carDatas.length;i++)    returnarr.push(this.carDatas[i].name);
    returnreturnarr;
  }
  
  /**
  @desc获得类别下的产品列表
  @returnArray
  */
  this.getPro=function(typeName){
    vartypePoint=this.getTypePoint(typeName);  if(typePoint==-1)returnfalse;  //没有此类别,返回假
    returnthis.carDatas[typePoint].value;
  }
  
  /**
  @desc获得商品属性
  @returnString
  */
  this.getProVal=function(typeName,proName){
    vartypePoint=this.getTypePoint(typeName);      if(typePoint==-1)returnfalse;  //没有此类别,返回假
    varproPoint=this.getProPoint(typePoint,proName);  if(proPoint==-1)returnfalse;  //没有此产品,返回假
    returnthis.carDatas[typePoint].value[proPoint].value;
  }
}

相关推荐

评论

展开