栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

Javascript操纵Cookie实现购物车程序

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Javascript操纵Cookie实现购物车程序

复制代码 代码如下:


function cookie(){
  
  this.setcookie=function(name, value, hours){
    var expire = "";
    if(hours != null){
      expire = new Date((new Date()).getTime() + hours * 3600000);
      expire = "; expires=" + expire.toGMTString();
    }
    document.cookie = escape(name) + "=" + escape(value) + expire;
  }
  
  
  this.getcookie=function(name){
    var cookievalue = "";
    var search = 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))
      }
    }
    return cookievalue;    
  }  
}

function Car(name){
  
  if( !window.clientInformation.cookieEnabled ) {
    alert('你的浏览器不支持cookie无法使用此 购物车 系统');
    return false;
  }
  
  //##内部变量#############################################################
  
  this.carName = name;
  this.expire   = 24*30;    //购物车的有效时间(30天)
  this.carDatas = new Array();
  this.cookie   = new cookie();
  
  //##内部对象#############################################################
  
  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
  //
  //########################################################################
  
  
  this.getTypePoint=function(typeName){
    var isok=false;
    var i=0;
    for(;i      if(this.carDatas[i].name==typeName){
        isok=true;      //找到位置
        break;
      }
    }
    if(isok)  return i;
    else    return -1;
  }
  
  
  this.getProPoint=function(typeId,proName){
    var isok=false;
    var j = 0;
    var tempProObj=this.carDatas[typeId].value;
    for(;j      if(tempProObj[j].name==proName){
        isok=true;
        break;  
      }
    }
    if(isok)  return j;
    else    return -1;
  }
  
  
  this.savecookie=function(){
    var outStr='';
    for( i=0; i      var typeName =this.carDatas[i].name;
      var typevalue=this.carDatas[i].value;
      var proOutStr='';
      for( j=0; 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{
    var tempTypes=this.cookie.getcookie(name).split(',');
    for(i=0;i      var tempTypeObj=tempTypes[i].split('#');
      var type_pro=new Array();
      if(tempTypeObj[1]){
        var tempProObj=tempTypeObj[1].split('|');
        for(j=0;j          var proDesc=tempProObj[j].split(':');
          type_pro.push(new this.proObj(proDesc[0],proDesc[1]));
        }
      }
      this.carDatas.push(new this.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);    //得到购物车内指定类别下的产品属性
  //
  //########################################################################
  
  
  this.addType=function(typeName){
    if(this.getTypePoint(typeName)!=-1)    return false;        //如果已经有此类别了,返回假
    this.carDatas.push(new this.typeObj(typeName,new Array()));      //push进 自身数组
    this.savecookie();  //存入 cookie
    return true;
  }
  
  
  this.addPro=function(typeName,proName,value){
    var typePoint=this.getTypePoint(typeName);      if ( typePoint ==-1 ) return false;    //没有此类别,无法增加,返回假
    var proPoint =this.getProPoint(typePoint,proName);  if ( proPoint != -1 ) return false;    //有此产品了,无法增加重复,返回假
    this.carDatas[typePoint].value.push(new this.proObj(proName,value));  //push到自身数组
    this.savecookie();  //存入 cookie
    return true;
  }
  
  
  this.editPro=function(typeName,proName,value){
    var typePoint=this.getTypePoint(typeName);      if ( typePoint == -1 ) return false;  //没有此类别,无法修改,返回假
    var proPoint =this.getProPoint(typePoint,proName);  if ( proPoint == -1 ) return false;  //没有此产品,无法修改,返回假
    this.carDatas[typePoint].value[proPoint].value=value;              //更新自身 
    this.savecookie();  //存入 cookie
    return true;
  }
  
  
  this.delPro=function(typeName,proName){
    var typePoint=this.getTypePoint(typeName);      if ( typePoint == -1 ) return false;  //没有此类别,无法删除,返回假
    var proPoint =this.getProPoint(typePoint,proName);  if ( proPoint == -1 ) return false;  //没有此产品,无法删除,返回假
    var pros=this.carDatas[typePoint].value.length;
    this.carDatas[typePoint].value[proPoint] = this.carDatas[typePoint].value[pros-1];  //最后一个产品放置要删除的产品上
    this.carDatas[typePoint].value.pop();
    this.savecookie();  //存入 cookie
    return true;
  }
  
  
  this.delType=function(typeName){
    var typePoint=this.getTypePoint(typeName);  if ( typePoint == -1 ) return false;  //没有此类别,无法删除,返回假
    var types=this.carDatas.length;
    this.carDatas[typePoint] = this.carDatas[types-1];            //删除类别
    this.carDatas.pop();
    this.savecookie();  //存入 cookie
    return true;
  }
  
  
  this.delCar=function(){
    this.cookie.setcookie(this.carName,'',0);
    this.carDatas=new Array();
    this.savecookie();  //存入 cookie
  }
  
  
  this.getCar=function(){
    return this.carDatas;
  }
  
  
  this.getType=function(){
    var returnarr=new Array();
    for ( i=0; i    return returnarr;
  }
  
  
  this.getPro=function(typeName){
    var typePoint=this.getTypePoint(typeName);  if ( typePoint == -1 ) return false;  //没有此类别,返回假
    return this.carDatas[typePoint].value;
  }
  
  
  this.getProVal=function(typeName,proName){
    var typePoint=this.getTypePoint(typeName);      if ( typePoint == -1 ) return false;  //没有此类别,返回假
    var proPoint =this.getProPoint(typePoint,proName);  if ( proPoint == -1 ) return false;  //没有此产品,返回假
    return this.carDatas[typePoint].value[proPoint].value;
  }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/120579.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号