JQuery implements the shopping cart Method Based on json and cookie, jqueryjson

Source: Internet
Author: User

JQuery implements the shopping cart Method Based on json and cookie, jqueryjson

This document describes how jQuery implements a shopping cart based on json and cookie. We will share this with you for your reference. The details are as follows:

Json format:

[{'ProductID':ABC','Num':'1'},{'ProductID':DEF,'Num':'2'}]

$. Cookie is used here. The code of this plug-in is at the end of the article

/* Add the item and quantity to the shopping cart cookie, and return the total number of items in the current cookie */function AddToShoppingCar (id, num) {var _ num = 1; if (num! = Undefined) _ num = num; var totalNum = _ num; // The default total number is the input parameter var cookieSet = {expires: 7, path :'/'}; // set the cookie Path // $. cookie (cookieProductID, null, cookieSet); // clear Cookie var jsonStr = "[{'produtid': '" + id + "', 'num ': '"+ _ num +"'}] "; // construct a json string. id is the product id. num is the quantity of the product. if ($. cookie (cookieProductID) = null) {$. cookie (cookieProductID, jsonStr, cookieSet); // if this cookie does not exist, set it} else {var jsonObj = eval ('+ $. cookie (cookieProductID) + '); // If yes, convert the json string to the object var findProduct = false; // whether the product ID is found, and TRUE if it is found, otherwise, it is FALSH for (var obj in jsonObj) {if (jsonObj [obj]. productID = id) {jsonObj [obj]. num = Number (jsonObj [obj]. num) + _ num; totalNum = jsonObj [obj]. num; findProduct = true; break;} if (findProduct = false) {// The jsonObj [jsonObj. length] = new Object (); jsonObj [jsonObj. length-1]. productID = id; jsonObj [jsonObj. length-1]. num = num;} $. cookie (cookieProductID, JSON. stringify (jsonObj), cookieSet); // write coockie JSON requires json2.js support} return totalNum; // alert ($. cookie (cookieProductID ));}
// The following is the cookie plug-in code jQuery. cookie = function (name, value, options) {if (typeof value! = 'Undefined') {// name and value given, set cookie options = options |{}; if (value = null) {value = ''; options. expires =-1;} var expires = ''; if (options. expires & (typeof options. expires = 'number' | options. expires. toUTCString) {var date; if (typeof options. expires = 'number') {date = new Date (); date. setTime (date. getTime () + (options. expires * 24*60*60*1000);} else {d Ate = options. expires;} expires = '; expires =' + date. toUTCString (); // use expires attribute, max-age is not supported by IE} var path = options. path? '; Path =' + options. path: ''; var domain = options. domain? '; Domain =' + options. domain: ''; var secure = options. secure? '; Secure': ''; document. cookie = [name, '=', encodeURIComponent (value), expires, path, domain, secure]. join ('');} else {// only name given, get cookie var cookieValue = null; if (document. cookie & document. cookie! = '') {Var cookies = document. cookie. split (';'); for (var I = 0; I <cookies. length; I ++) {var cookie = jQuery. trim (cookies [I]); // Does this cookie string begin with the name we want? If (cookie. substring (0, name. length + 1) = (name + '=') {cookieValue = decodeURIComponent (cookie. substring (name. length + 1); break ;}}return cookieValue ;}};

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.