/************************************************ *************************************************** ********
Name Shopping Cart
Version 1.1
Author Vanni(Fan Lin) url:www.27sea.com QQ:303590170
CreateDate 2005-05-31
Description
This class is based on JavaScript and client cookies, please ensure that the client turns on Cookie
Data retention (default 24*30 hours) can be specified by this.expire=? Hours
Two of the classes come with it The two objects typeObj and proObj have two identical attribute names: name and value The data storage form in the
class is as follows----------------------------- ---------------
Array(
new typeObj('Car',array(
new porObj('Mitsubishi',200),
new proObj ('Honda',500)
)
),
new typeObj('Egg', array(
,20)
)
}
Cookie access form is [encrypted using escape() function]--------------
Shopping cart Name = car # Mitsubishi: 200 | Honda: 500, egg # egg: 10 | duck egg: 20
Note: There will be no problem when the client saves cookies.If you want to store in a loop, some may be saved and some may not be saved
Solution: See the example below (get the number of sales in the URL and save it in the cookie)
File: / JS code snippet in depot/compareproduct.php
*****************************************************************************************************/
/**
Cookie class
*/
function Cookie(){
/**
@desc Set 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 Read 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){ //自带的" 商品 对象
this.name =name;
this.value=value;
}
//##私有方法列表##########################################################
//
// getTypePoint(typeName); //得到购物车里类别数组里的下标
// getProPoint(typeName,proName); //得到购物车里类别下的产品下标
// saveCookie() //以特定的形式存储此购物车的Cookie
//
//########################################################################
/**
@desc Get the subscript in the category array in the shopping cart. If found, return the subscript, otherwise return -1
@return int
*/
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;
}
/**
@desc Get the product subscript under the category in the shopping cart, find the return subscript, otherwise return -1
@return int
*/
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;
}
/**
@desc Stores the generated Cookie string
@return void
*/
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); //得到购物车内指定类别下的产品属性
//
//########################################################################
/**
@desc Add a category to the shopping cart and return true if successful, otherwise false
@return bool
*/
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;
}
/**
@desc Add a product to the shopping cart and return true if successful, otherwise false
@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; //有此产品了,无法增加重复,返回假
this.carDatas[typePoint].value.push(new this.proObj(proName,value)); //push到自身数组
this.saveCookie(); //存入 Cookie
return true;
}
/**
@desc 장바구니의 상품 속성 수정
@return bool
*/
this.editPro=function(typeName,proName,value){
var typePoint=this.getTypePoint(typeName); if ( typePoint == -1 ) return false; //해당 카테고리가 없으며 수정할 수 없습니다. return false
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false
This .carDatas[typePoint].value[proPoint].value=value; //자체 업데이트
this.saveCookie(); //쿠키 저장
return true; *
@desc 제품 삭제
@return bool
*/
this.delPro=function(typeName,proName){
var typePoint=this.getTypePoint(typeName); if (typePoint == -1) return false ; 카테고리, 삭제할 수 없음, false 반환
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false //해당 상품이 없으므로 삭제할 수 없음, false 반환
var pros=this.carDatas[typePoint].value.length;
this.carDatas[typePoint].value[proPoint] = this.carDatas[typePoint].value[pros-1]; product 삭제할 상품을 배치하세요
This.carDatas[typePoint].value.pop(); //쿠키 저장
return true; 🎜> /**
@desc 카테고리 삭제
@return bool
*/
this.delType=function(typeName){
var typePoint=this.getTypePoint(typeName); if (typePoint == -1) return false; 해당 카테고리가 없으면 false를 반환합니다
var type=this.carDatas.length;
this.carDatas[typePoint] = this.carDatas[types-1]; //카테고리 삭제
this. carDatas.pop ()
this.saveCookie(); //쿠키 저장
return true
/**
@desc 장바구니 삭제
@return void
*/
this.delCar= function( ){
This.cookie.setCookie(this.carName,'',0);
this.carDatas=new Array()
this.saveCookie(); > }
/**
@desc 장바구니 데이터 가져오기
@return 배열
*/
this.getCar=function(){
return this.carDatas;
}
/**
@desc 카테고리 목록 가져오기
@return Array
*/
this.getType=function(){
var returnarr=new Array();
for ( i=0; i returnarr
}
/**
@desc 카테고리
@return Array
에서 제품 목록 가져오기*/
this.getPro=function(typeName){
var typePoint= this .getTypePoint(typeName); if (typePoint == -1) return false; //해당 카테고리가 없으면 return false
return this.carDatas[typePoint].value; > /* *
@desc 제품 속성 가져오기
@return 문자열
*/
This.getProVal=function(typeName,proName){
var typePoint=this.getTypePoint(typeName); if (typePoint == -1) return false //거기 해당 카테고리가 없으면 Return false
var proPoint = this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false // 해당 제품이 없습니다. return false
return this.carDatas [typePoint].value[proPoint ].value
}
}