色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Javascript操縱Cookie實(shí)現(xiàn)購物車程序

復(fù)制代碼 代碼如下:
/*****************************************************************************************************
Name    購物車
Version    1.1
Author    Vanni(凡林) url:www.27sea.com QQ:303590170
CreateDate  2005-05-31
Description
  此類是基于JavaScript和客戶端Cookie,請保證客戶端開啟Cookie
  數(shù)據(jù)保持(默認(rèn)24*30小時(shí))可以通過 this.expire=? 小時(shí)來指定
  類中兩自帶的兩個(gè)對象 typeObj 和 proObj 均有兩個(gè)相同屬性名: name 和 value 

類中數(shù)據(jù)存儲(chǔ)形式如下-----------------------------------
Array(
new typeObj('汽車',array(
    new porObj('三菱',200),
    new proObj('本田',500)
)
  ),
  new typeObj('蛋',array(
    new proObj('雞蛋',10),
    new proObj('鴨蛋',20)
  )
}

Cookie 存取形式為[使用escape()函數(shù)加密過]--------------
  購物車名 = 汽車#三菱:200|本田:500,蛋#雞蛋:10|鴨蛋:20

注意:客戶端存Cookie時(shí),不會(huì)出現(xiàn)問題。如果要循環(huán)存儲(chǔ)的話,可能會(huì)出現(xiàn)有些存入,而有些未存入
   解決方法:見下例(獲得URL里的sales的數(shù)量,并存入Cookie)

文件:/depot/compareproduct.php 中的JS代碼片段
<script language="Javascript">
var car=new Car('compare');
var typeName='list';
car.delType(typeName);    //將先前對比的產(chǎn)品清除

//得到URL里的參數(shù),并分隔成數(shù)組
var url=location.href;
var start=url.lastIndexOf('?sales=');
var end=url.indexOf('&');
if(end==-1)end=url.length;
var urlparam=url.substring(url.lastIndexOf('?sales=')+7, end ).split(',');

function setPageVal(){
  if(car.getPro(typeName).length==urlparam.length)return;    //關(guān)鍵部分,如果數(shù)組長度不相等說明,有些Cookie沒有存入
  else{
    car.addType(typeName);            //增一個(gè)類別
    for(i=0;i<urlparam.length;i++){
      car.addPro(typeName,urlparam[i],'');  //增加對比產(chǎn)品,如果存在,返回假
    }
    setTimeout('setPageVal();',100);      //再次調(diào)用自身,沒有用遞歸,是因?yàn)檫f歸速度太快,仍會(huì)有存不進(jìn)的問題
  }
}

setPageVal();                    //初始化數(shù)據(jù)

function delItem(itemname){
  car.delPro(typeName,itemname);
  var carData=car.getPro(typeName);
  var url='';
  var carlen=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('如果刪除它,那么只剩一個(gè)對比項(xiàng)了,是否關(guān)閉此窗口?')){
      car.delCar();
      window.close();
    }
  }
}
</script>

*****************************************************************************************************/
/**
Cookie類
*/
function Cookie(){
  /**
  @desc 設(shè)置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無法使用此 購物車 系統(tǒng)');
    return false;
  }
  
  //##內(nèi)部變量#############################################################
  
  this.carName = name;
  this.expire   = 24*30;    //購物車的有效時(shí)間(30天)
  this.carDatas = new Array();
  this.cookie   = new Cookie();
  
  //##內(nèi)部對象#############################################################
  
  this.typeObj=function(name,value){  //自帶的 類別 對象
    this.name =name;
    this.value="/value;
  }
  this.proObj=function(name,value){  //自帶的" 商品 對象
    this.name =name;
    this.value=value;
  }
  
  //##私有方法列表##########################################################
  //
  //  getTypePoint(typeName);        //得到購物車?yán)镱悇e數(shù)組里的下標(biāo)
  //  getProPoint(typeName,proName);    //得到購物車?yán)镱悇e下的產(chǎn)品下標(biāo)
  //  saveCookie()            //以特定的形式存儲(chǔ)此購物車的Cookie
  //
  //########################################################################
  
  /**
  @desc 得到購物車?yán)镱悇e數(shù)組里的下標(biāo),找到的話返回下標(biāo),否則返回 -1
  @return int
  */
  this.getTypePoint=function(typeName){
    var isok=false;
    var i=0;
    for(;i<this.carDatas.length;i++){
      if(this.carDatas[i].name==typeName){
        isok=true;      //找到位置
        break;
      }
    }
    if(isok)  return i;
    else    return -1;
  }
  
  /**
  @desc 得到購物車?yán)镱悇e下的產(chǎn)品下標(biāo),找到返回下標(biāo),否則返回 -1
  @return int
  */
  this.getProPoint=function(typeId,proName){
    var isok=false;
    var j = 0;
    var tempProObj=this.carDatas[typeId].value;
    for(;j<tempProObj.length;j++){
      if(tempProObj[j].name==proName){
        isok=true;
        break;  
      }
    }
    if(isok)  return j;
    else    return -1;
  }
  
  /**
  @desc 存儲(chǔ)生成的Cookie字符串
  @return void
  */
  this.saveCookie=function(){
    var outStr='';
    for( i=0; i<this.carDatas.length; i++ ){
      var typeName =this.carDatas[i].name;
      var typeValue=this.carDatas[i].value;
      var proOutStr='';
      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  
  }
    
  //##構(gòu)造語句############################################################
  
  if(this.cookie.getCookie(name)==''){
    this.cookie.setCookie(name,'',this.expire);
  }else{
    var tempTypes=this.cookie.getCookie(name).split(',');
    for(i=0;i<tempTypes.length;i++){
      var tempTypeObj=tempTypes[i].split('#');
      var type_pro=new Array();
      if(tempTypeObj[1]){
        var tempProObj=tempTypeObj[1].split('|');
        for(j=0;j<tempProObj.length;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);          //增加一個(gè)類別
  //  addPro(typeName,proName,value);    //增加一個(gè)產(chǎn)品
  //  editPro(typeName,proName,value);  //修改產(chǎn)品的值
  //  delPro(typeName,proName);      //刪除購物車內(nèi)的一個(gè)類別下的產(chǎn)品
  //  delType(typeName);          //刪除購物車內(nèi)的一個(gè)類別,包括類別下的產(chǎn)品
  //  delCar();              //刪除購物車
  //  
  //  getCar();              //得到整個(gè)購物車的數(shù)據(jù)
  //  getType();              //得到購物車內(nèi)的所有類別列表
  //  getPro(typeName);          //得到購物車內(nèi)指定類別下的產(chǎn)品列表
  //  getProVal(typeName,proName);    //得到購物車內(nèi)指定類別下的產(chǎn)品屬性
  //
  //########################################################################
  
  /**
  @desc 在購物車?yán)镌黾右粋€(gè)類別,增加成功返回真,否則返回假
  @return bool
  */
  this.addType=function(typeName){
    if(this.getTypePoint(typeName)!=-1)    return false;        //如果已經(jīng)有此類別了,返回假
    this.carDatas.push(new this.typeObj(typeName,new Array()));      //push進(jìn) 自身數(shù)組
    this.saveCookie();  //存入 Cookie
    return true;
  }
  
  /**
  @desc 在購物車?yán)镌黾右粋€(gè)產(chǎn)品,增加成功返回真,否則返回假
  @return bool
  */
  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;    //有此產(chǎn)品了,無法增加重復(fù),返回假
    this.carDatas[typePoint].value.push(new this.proObj(proName,value));  //push到自身數(shù)組
    this.saveCookie();  //存入 Cookie
    return true;
  }
  
  /**
  @desc 修改購物車?yán)锏漠a(chǎn)品屬性
  @return bool
  */
  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;  //沒有此產(chǎn)品,無法修改,返回假
    this.carDatas[typePoint].value[proPoint].value=value;              //更新自身 
    this.saveCookie();  //存入 Cookie
    return true;
  }
  
  /**
  @desc 刪除一個(gè)產(chǎn)品
  @return bool
  */
  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;  //沒有此產(chǎn)品,無法刪除,返回假
    var pros=this.carDatas[typePoint].value.length;
    this.carDatas[typePoint].value[proPoint] = this.carDatas[typePoint].value[pros-1];  //最后一個(gè)產(chǎn)品放置要?jiǎng)h除的產(chǎn)品上
    this.carDatas[typePoint].value.pop();
    this.saveCookie();  //存入 Cookie
    return true;
  }
  
  /**
  @desc 刪除一個(gè)類別
  @return bool
  */
  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;
  }
  
  /**
  @desc 刪除此購物車
  @return void
  */
  this.delCar=function(){
    this.cookie.setCookie(this.carName,'',0);
    this.carDatas=new Array();
    this.saveCookie();  //存入 Cookie
  }
  
  /**
  @desc 獲得購物車數(shù)據(jù)
  @return Array
  */
  this.getCar=function(){
    return this.carDatas;
  }
  
  /**
  @desc 獲得類別列表
  @return Array
  */
  this.getType=function(){
    var returnarr=new Array();
    for ( i=0; i<this.carDatas.length; i++)    returnarr.push(this.carDatas[i].name);
    return returnarr;
  }
  
  /**
  @desc 獲得類別下的產(chǎn)品列表
  @return Array
  */
  this.getPro=function(typeName){
    var typePoint=this.getTypePoint(typeName);  if ( typePoint == -1 ) return false;  //沒有此類別,返回假
    return this.carDatas[typePoint].value;
  }
  
  /**
  @desc 獲得商品屬性
  @return String
  */
  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;  //沒有此產(chǎn)品,返回假
    return this.carDatas[typePoint].value[proPoint].value;
  }
}

JavaScript技術(shù)Javascript操縱Cookie實(shí)現(xiàn)購物車程序,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 寻甸| 临桂县| 大石桥市| 齐齐哈尔市| 哈巴河县| 高邮市| 波密县| 雷山县| 中超| 平原县| 屯昌县| 通渭县| 定远县| 洪泽县| 新平| 余庆县| 福建省| 上犹县| 湟中县| 满城县| 晴隆县| 土默特左旗| 建昌县| 兴仁县| 太白县| 漯河市| 囊谦县| 庄河市| 东乌珠穆沁旗| 集安市| 林甸县| 资中县| 和林格尔县| 临颍县| 玉山县| 探索| 吴堡县| 乐安县| 霍林郭勒市| 都安| 呼伦贝尔市|