JavascriptはCookieを操作してカートプログラムを実現します.
13185 ワード
/*****************************************************************************************************
Name
Version 1.1
Author Vanni( ) url:www.27sea.com QQ:303590170
CreateDate 2005-05-31
Description
JavaScript Cookie, Cookie
( 24*30 ) this.expire=?
typeObj proObj : name value
-----------------------------------
Array(
new typeObj(' ',array(
new porObj(' ',200),
new proObj(' ',500)
)
),
new typeObj(' ',array(
new proObj(' ',10),
new proObj(' ',20)
)
}
Cookie [ escape() ]--------------
= # :200| :500, # :10| :20
: Cookie , 。 , ,
: ( URL sales , Cookie)
:/depot/compareproduct.php JS
<br>var car=new Car('compare');
<br>var typeName='list';
<br>car.delType(typeName); //
<br>
<br>// URL ,
<br>var url=location.href;
<br>var start=url.lastIndexOf('?sales=');
<br>var end=url.indexOf('&');
<br>if(end==-1)end=url.length;
<br>var urlparam=url.substring(url.lastIndexOf('?sales=')+7, end ).split(',');
<br>
<br>function setPageVal(){
<br> if(car.getPro(typeName).length==urlparam.length)return; // , , Cookie
<br> else{
<br> car.addType(typeName); //
<br> for(i=0;i<urlparam.length;i++){
<br> car.addPro(typeName,urlparam[i],''); // , ,
<br> }
<br> setTimeout('setPageVal();',100); // , , ,
<br> }
<br>}
<br>
<br>setPageVal(); //
<br>
<br>function delItem(itemname){
<br> car.delPro(typeName,itemname);
<br> var carData=car.getPro(typeName);
<br> var url='';
<br> var carlen=carData.length;
<br> if(carlen>1){
<br> for(i=0;i<carData.length;i++){
<br> if(i==0) url =carData[i].name;
<br> else url+=','+carData[i].name;
<br> }
<br> document.write("waiting....");
<br> location.href='../depot/compareproduct.php?sales='+url;
<br> }else{
<br> if(confirm(' , , ?')){
<br> car.delCar();
<br> window.close();
<br> }
<br> }
<br>}
<br>
*****************************************************************************************************/
/**
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){ // "
this.name =name;
this.value=value;
}
//## ##########################################################
//
// getTypePoint(typeName); //
// getProPoint(typeName,proName); //
// saveCookie() // Cookie
//
//########################################################################
/**
@desc , , -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 , , -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 Cookie
@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 , ,
@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 , ,
@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; // , ,
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false; // , ,
this.carDatas[typePoint].value[proPoint].value=value; //
this.saveCookie(); // Cookie
return true;
}
/**
@desc
@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; // , ,
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;
}
/**
@desc
@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
@return Array
*/
this.getCar=function(){
return this.carDatas;
}
/**
@desc
@return Array
*/
this.getType=function(){
var returnarr=new Array();
for ( i=0; i return returnarr;
}
/**
@desc
@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; // ,
return this.carDatas[typePoint].value[proPoint].value;
}
}