Using Javascript to manipulate cookies to implement the Shopping Cart program

Source: Internet
Author: User

Copy codeThe Code is 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. please ensure that the client enables cookies
Data persistence (24*30 hours by default) can be performed through this. expire =? Hours to specify
The two built-in objects typeObj and proObj have two identical attribute names: name and value.

The data storage format in the class is as follows -----------------------------------
Array (
New typeObj ('auto', array (
New porObj ('mitsubishi, 200 ),
New proObj ('honda ', 500)
)
),
New typeObj ('eg', array (
New proObj ('eg', 10 ),
New proObj ('duck eg', 20)
)
}

The Cookie Access format is [encrypted using the escape () function] ------------
Shopping Cart name = car # Mitsubishi: 200 | Honda: 500, eggs # eggs: 10 | duck eggs: 20

Note: When the client saves the Cookie, no problem occurs. If you want to store them cyclically, some may be saved, while some are not saved.
Solution: See the following example (obtain the sales quantity in the URL and store the Cookie)

File: JS code snippets in/depot/compareproduct. php
<Script language = "javascript">
Var car = new Car ('compare ');
Var typeName = 'LIST ';
Car. delType (typeName); // clear the previously compared products

// Obtain the parameters in the URL and separate them into arrays.
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], ''); // adds a comparison product. If yes, false is returned.
}
SetTimeout ('setpageval (); ', 100); // call itself again. recursion is not used because the recursion speed is too fast and there will still be problems
}
}

SetPageVal (); // initialize 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, there is only one comparison item left. Close this window? ')){
Car. delCar ();
Window. close ();
}
}
}
</Script>

**************************************** **************************************** *********************/
/**
Cookie type
*/
Function Cookie (){
/**
@ Desc set Cookie
@ Return void
*/
This. setCookie = function (name, value, hours ){
Var expire = "";
If (hours! = Null ){
Expire = 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 ('the Shopping Cart system cannot be used because your browser does not support cookies ');
Return false;
}
  
// ### Internal variables ################################## ###########################
  
This. carName = name;
This. expire = 24*30; // validity period of the shopping cart (30 days)
This. carDatas = new Array ();
This. cookie = new Cookie ();
  
// ### Internal object ################################## ###########################
  
This. typeObj = function (name, value) {// Class Object
This. name = name;
This. value = "/value;
}
This. proObj = function (name, value) {// comes with the "Product object"
This. name = name;
This. value = value;
}
  
// ### List of private methods ################################# #########################
//
// GetTypePoint (typeName); // obtain the subscript of the category array in the shopping cart.
// GetProPoint (typeName, proName); // obtain the product subscript under the category in the shopping cart.
// SaveCookie () // store the Cookie of this shopping cart in a specific form
//
//###################################### ##################################
  
/**
@ Desc: Obtain the subscript in the category array in the shopping cart. If it is found, the subscript is returned. Otherwise,-1 is returned.
@ 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; // locate
Break;
}
}
If (isok) return I;
Else return-1;
}
  
/**
@ Desc get the product subscript under the category in the shopping cart and find the return subscript; otherwise,-1 is returned.
@ 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 stores 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); // Save the Cookie
}
    
// ### Construct a statement ################################## ##########################
  
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 ));
}
}

// ### Public method list ################################# ########################
//
// AddType (typeName); // Add a category
// AddPro (typeName, proName, value); // Add a product
// EditPro (typeName, proName, value); // modify the product value
// DelPro (typeName, proName); // delete a product under a category in the shopping cart
// DelType (typeName); // delete a category in the shopping cart, including the product under the category
// DelCar (); // delete a shopping cart
//
// GetCar (); // obtain the data of the entire shopping cart.
// GetType (); // obtain the list of all categories in the cart.
// GetPro (typeName); // get the list of products under the specified category in the shopping cart
// GetProVal (typeName, proName); // get the product attributes under the specified category in the shopping cart
//
//###################################### ##################################
  
/**
@ Desc adds a category to the shopping cart and returns true if the category is added successfully. Otherwise, false is returned.
@ Return bool
*/
This. addType = function (typeName ){
If (this. getTypePoint (typeName )! =-1) return false; // if this category already exists, false is returned.
This. carDatas. push (new this. typeObj (typeName, new Array (); // push the object to its own Array
This. saveCookie (); // Save the Cookie
Return true;
}
  
/**
@ Desc Add a product to the shopping cart. if the product is added to the shopping cart, the returned result is true. Otherwise, the returned result is false.
@ Return bool
*/
This. addPro = function (typeName, proName, value ){
Var typePoint = this. getTypePoint (typeName); if (typePoint =-1) return false; // if this category does not exist, cannot be added, and false is returned
Var proPoint = this. getProPoint (typePoint, proName); if (proPoint! =-1) return false; // if this product exists, the duplicate cannot be added, and false is returned.
This. carDatas [typePoint]. value. push (new this. proObj (proName, value); // push to its own array
This. saveCookie (); // Save the Cookie
Return true;
}
  
/**
@ Desc modify the product attributes in the shopping cart
@ Return bool
*/
This. editPro = function (typeName, proName, value ){
Var typePoint = this. getTypePoint (typeName); if (typePoint =-1) return false; // if this category does not exist, it cannot be modified, and false is returned.
Var proPoint = this. getProPoint (typePoint, proName); if (proPoint =-1) return false; // if this product does not exist, it cannot be modified and false is returned
This. carDatas [typePoint]. value [proPoint]. value = value; // update itself
This. saveCookie (); // Save the Cookie
Return true;
}
  
/**
@ Desc delete a product
@ Return bool
*/
This. delPro = function (typeName, proName ){
Var typePoint = this. getTypePoint (typeName); if (typePoint =-1) return false; // if this category does not exist, it cannot be deleted, and false is returned.
Var proPoint = this. getProPoint (typePoint, proName); if (proPoint =-1) return false; // if this product does not exist, it cannot be deleted, and false is returned
Var pros = this. carDatas [typePoint]. value. length;
This. carDatas [typePoint]. value [proPoint] = this. carDatas [typePoint]. value [pros-1]; // place the last product on the product to be deleted
This. carDatas [typePoint]. value. pop ();
This. saveCookie (); // Save the Cookie
Return true;
}
  
/**
@ Desc delete a category
@ Return bool
*/
This. delType = function (typeName ){
Var typePoint = this. getTypePoint (typeName); if (typePoint =-1) return false; // if this category does not exist, it cannot be deleted, and false is returned.
Var types = this. carDatas. length;
This. carDatas [typePoint] = this. carDatas [types-1]; // delete a category
This. carDatas. pop ();
This. saveCookie (); // Save the Cookie
Return true;
}
  
/**
@ Desc Delete this shopping cart
@ Return void
*/
This. delCar = function (){
This. cookie. setCookie (this. carName, '', 0 );
This. carDatas = new Array ();
This. saveCookie (); // Save the Cookie
}
  
/**
@ Desc get shopping cart data
@ Return Array
*/
This. getCar = function (){
Return this. carDatas;
}
  
/**
@ Desc get the category list
@ 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 the product list under the category
@ Return Array
*/
This. getPro = function (typeName ){
Var typePoint = this. getTypePoint (typeName); if (typePoint =-1) return false; // if this category does not exist, false is returned.
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; // if this category does not exist, false is returned.
Var proPoint = this. getProPoint (typePoint, proName); if (proPoint =-1) return false; // if this product does not exist, false is returned.
Return this. carDatas [typePoint]. value [proPoint]. value;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.