亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2007年02月15日 00:00:00   作者:  
復(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ù)存儲形式如下-----------------------------------
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í),不會出現(xiàn)問題。如果要循環(huán)存儲的話,可能會出現(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歸速度太快,仍會有存不進(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()            //以特定的形式存儲此購物車的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 存儲生成的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;
  }
}

相關(guān)文章

  • 前端JavaScript獲取電池信息

    前端JavaScript獲取電池信息

    受到同事啟發(fā),突然發(fā)現(xiàn)了幾個(gè)有趣又實(shí)用的web api,下面這篇文章主要給大家介紹了關(guān)于前端JavaScript獲取電池信息的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • javascript表單驗(yàn)證大全

    javascript表單驗(yàn)證大全

    JavaScript是用來在數(shù)據(jù)被傳輸?shù)椒?wù)前對html表單中輸入的數(shù)據(jù)進(jìn)行驗(yàn)證,使用javascript對用戶輸入的信息進(jìn)行驗(yàn)證是項(xiàng)目必須的,下面小編給大家整理一些比較常用的javascript表單驗(yàn)證,需要的朋友可以參考下
    2015-08-08
  • JavaScript面試必考之實(shí)現(xiàn)手寫Promise

    JavaScript面試必考之實(shí)現(xiàn)手寫Promise

    Promise作為面試必考題,Promise的手寫也是面試官必問的問題,所以對于Promise我們一定要了解透徹。本文就為大家整理了手寫Promise的示例代碼,需要的可以參考一下
    2022-12-12
  • js將圖片轉(zhuǎn)base64的兩種實(shí)現(xiàn)方法

    js將圖片轉(zhuǎn)base64的兩種實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于js將圖片轉(zhuǎn)base64的兩種實(shí)現(xiàn)方法,Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來表示二進(jìn)制數(shù)據(jù)的方法,需要的朋友可以參考下
    2023-07-07
  • javascript獲取選中的文本的方法代碼

    javascript獲取選中的文本的方法代碼

    這篇文章介紹了javascript獲取選中的文本的方法代碼,有需要的朋友可以參考一下
    2013-10-10
  • 用Javascript數(shù)組處理多個(gè)字符串的連接問題

    用Javascript數(shù)組處理多個(gè)字符串的連接問題

    小技巧 用Javascript數(shù)組處理多個(gè)字符串的連接問題,大家可以參考下。
    2009-08-08
  • JS模擬實(shí)現(xiàn)Select效果代碼

    JS模擬實(shí)現(xiàn)Select效果代碼

    這篇文章主要介紹了JS模擬實(shí)現(xiàn)Select效果代碼,涉及JavaScript基于鼠標(biāo)點(diǎn)擊事件動態(tài)操作頁面元素實(shí)現(xiàn)Select效果的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • JavaScript 處理樹數(shù)據(jù)結(jié)構(gòu)的方法示例

    JavaScript 處理樹數(shù)據(jù)結(jié)構(gòu)的方法示例

    這篇文章主要介紹了JavaScript 處理樹數(shù)據(jù)結(jié)構(gòu)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-06-06
  • Js 獲取HTML DOM節(jié)點(diǎn)元素的方法小結(jié)

    Js 獲取HTML DOM節(jié)點(diǎn)元素的方法小結(jié)

    在Web應(yīng)用程序特別是Web2.0程序開發(fā)中,經(jīng)常要獲取頁面中某個(gè)元素,然后更新該元素的樣式、內(nèi)容等。
    2009-04-04
  • 詳解Javacript和AngularJS中的Promises

    詳解Javacript和AngularJS中的Promises

    這篇文章主要介紹了詳解Javacript和AngularJS中的Promises的相關(guān)資料,promise是Javascript異步編程很好的解決方案。,需要的朋友可以參考下
    2016-02-02

最新評論