Copy Code code as follows:
/*****************************************************************************************************
Name Shopping Cart
Version 1.1
Author Vanni (Fanlin) url:www.27sea.com qq:303590170
CreateDate 2005-05-31
Description
This class is based on JavaScript and client cookies, make sure the client opens cookies
Data retention (default 24*30 hours) is available through this.expire=? Hours to specify
Typeobj and Proobj have two identical property names for both the two objects in the class: Name and Value
The data in a class is stored in the following form-----------------------------------
Array (
New Typeobj (' Auto ', array (
New Porobj (' Mitsubishi ', 200),
New Proobj (' Honda ', 500)
)
),
New Typeobj (' Egg ', array (
New Proobj (' egg ', 10),
New Proobj (' duck eggs ', 20)
)
}
The Cookie Access form is encrypted using the escape () function--------------
Shopping Cart name = car # Mitsubishi: 200| Honda: 500, Egg # Egg: 10| duck eggs: 20
Note: The problem does not occur when the client saves cookies. If you want to cycle storage, there may be some deposits, while some are not deposited
Workaround: See the following example (get the number of sales in the URL and save it in a cookie)
File: JS code fragment in/depot/compareproduct.php
<script language= "JavaScript" >
var car=new car (' compare ');
var typename= ' list ';
Car.deltype (TypeName); Clear the previously contrasted product
Get the parameters in the URL and separate the groups
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; Key part, if the array length is not equal, some cookies are not saved
else{
Car.addtype (TypeName); Add a Category
for (i=0;i<urlparam.length;i++) {
Car.addpro (Typename,urlparam[i], ""); Add contrast product if present, return fake
}
SetTimeout (' Setpageval (); ', 100); Call itself again, not with recursion, because recursion is too fast, there will still be problems
}
}
Setpageval (); Initializing data
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 (' If you delete it, then only one comparison is left, do you want to close this window? ')){
Car.delcar ();
Window.close ();
}
}
}
</script>
*****************************************************************************************************/
/**
Cookie class
*/
function Cookie () {
/**
@desc Set Cookies
@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 Cookies
@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 (' Your browser does not support cookies and cannot use this cart system ');
return false;
}
# #内部变量 #############################################################
This.carname = name;
This.expire = 24*30; Effective time of shopping cart (30 days)
This.cardatas = new Array ();
This.cookie = new Cookie ();
# #内部对象 #############################################################
This.typeobj=function (Name,value) {//self-with Category object
THIS.name =name;
This.value= "/value;
}
This.proobj=function (Name,value) {//self-with "commodity object"
THIS.name =name;
This.value=value;
}
# #私有方法列表 ##########################################################
//
Gettypepoint (TypeName); Get the subscript in the category array in the shopping cart
Getpropoint (Typename,proname); Get the product subscript under the category in the shopping cart
Savecookie ()//store cookies for this cart in a specific form
//
//########################################################################
/**
@desc get the subscript in the category array in the cart, return the subscript if found, or return-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; Find a location
Break
}
}
if (isOK) return i;
else return-1;
}
/**
@desc get the product subscript under the category of the shopping cart, find the return subscript, or return-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 store the generated cookie string
@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); Depositing cookies
}
# #构造语句 ############################################################
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); Add a Category
Addpro (Typename,proname,value); Add a Product
Editpro (Typename,proname,value); Modify the value of a product
Delpro (Typename,proname); Delete a product under a category in a shopping cart
Deltype (TypeName); Delete a category in the cart, including the product under the category
Delcar (); Delete Shopping Cart
//
Getcar (); Get the whole shopping cart data
GetType (); Get a list of all the categories in the shopping cart
Getpro (TypeName); Get a list of products under the specified category in the shopping cart
Getproval (Typename,proname); Get the product properties under the specified category in the shopping cart
//
//########################################################################
/**
@desc Add a category to the cart, increase the success return true, otherwise return the fake
@return BOOL
*/
This.addtype=function (typeName) {
if (This.gettypepoint (typeName)!=-1) return false; If you already have this category, return a fake
This.carDatas.push (New This.typeobj (Typename,new Array ())); Push into its own array
This.savecookie (); Depositing cookies
return true;
}
/**
@desc Add a product to the shopping cart, increase the success return true, otherwise return false
@return BOOL
*/
This.addpro=function (Typename,proname,value) {
var typepoint=this.gettypepoint (typeName); if (Typepoint ==-1) return false; No this category, cannot increase, return false
var propoint =this.getpropoint (Typepoint,proname); if (Propoint!=-1) return false; Have this product, cannot add duplicate, return false
This.cardatas[typepoint].value.push (New This.proobj (Proname,value)); Push to its own array
This.savecookie (); Depositing cookies
return true;
}
/**
@desc Modify product properties in a shopping cart
@return BOOL
*/
This.editpro=function (Typename,proname,value) {
var typepoint=this.gettypepoint (typeName); if (Typepoint = = 1) return false; No such category, cannot modify, return False
var propoint =this.getpropoint (Typepoint,proname); if (Propoint = = 1) return false; No this product, cannot modify, return False
This.cardatas[typepoint].value[propoint].value=value; Update itself
This.savecookie (); Depositing cookies
return true;
}
/**
@desc Delete a product
@return BOOL
*/
This.delpro=function (typename,proname) {
var typepoint=this.gettypepoint (typeName); if (Typepoint = = 1) return false; No such category, cannot delete, return false
var propoint =this.getpropoint (Typepoint,proname); if (Propoint = = 1) return false; No this product, cannot delete, return fake
var pros=this.cardatas[typepoint].value.length;
This.cardatas[typepoint].value[propoint] = this.cardatas[typepoint].value[pros-1]; The last product to put on the product to be deleted
This.cardatas[typepoint].value.pop ();
This.savecookie (); Depositing cookies
return true;
}
/**
@desc Delete a category
@return BOOL
*/
This.deltype=function (typeName) {
var typepoint=this.gettypepoint (typeName); if (Typepoint = = 1) return false; No such category, cannot delete, return false
var types=this.cardatas.length;
This.cardatas[typepoint] = this.cardatas[types-1]; Delete Category
This.carDatas.pop ();
This.savecookie (); Depositing cookies
return true;
}
/**
@desc Delete this cart
@return void
*/
This.delcar=function () {
This.cookie.setCookie (This.carname, ', 0);
This.cardatas=new Array ();
This.savecookie (); Depositing cookies
}
/**
@desc get the shopping cart data
@return Array
*/
This.getcar=function () {
return this.cardatas;
}
/**
@desc get a list of categories
@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 get a list of products under the category
@return Array
*/
This.getpro=function (typeName) {
var typepoint=this.gettypepoint (typeName); if (Typepoint = = 1) return false; Without this category, returns a false
return this.cardatas[typepoint].value;
}
/**
@desc Get Product attributes
@return String
*/
This.getproval=function (typename,proname) {
var typepoint=this.gettypepoint (typeName); if (Typepoint = = 1) return false; Without this category, returns a false
var propoint =this.getpropoint (Typepoint,proname); if (Propoint = = 1) return false; Without this product, return a fake
return this.cardatas[typepoint].value[propoint].value;
}
}