Javascript操纵Cookie实现购物车程序

2019-06-03 06:02:56于海丽

    }
    document.write("waiting....");
    location.href='../depot/compareproduct.php?sales='+url;
  }else{
    if(confirm('如果删除它,那么只剩一个对比项了,是否关闭此窗口?')){
      car.delCar();
      window.close();
    }
  }
}
</script>

*****************************************************************************************************/
/**
Cookie类
*/
function Cookie(){
  /**
  @desc 设置Cookie
  @return void
  */
  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;
  }
  
  /**
  @desc 读取Cookie
  @return String
  */
  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){  //自带的" 商品 对象