Localstorage value and Access JSON, and shopping carts based on HTML5 localstorage __json

Source: Internet
Author: User
Tags convert string to json convert string to json object string to json
Localstorage.setitem ("Key", "value");//store variable named key, value
 
= Localstorage.key = "value"//save variable named key, value is
 
Localstorage.getitem ("key");//Gets the value of the stored variable key www.it165.net
 
localstorage.key;//Gets the value
 
of the stored variable key Localstorage.removeitem ("key")//delete stored variable named key
The above is the method called by Localstorage. The following is a description of how to store a JSON object.
var students = 
{
	liyang:{name: "Liyang", age:17},

	lilei:{name: "Lilei", age:18}

}//the JSON object to store


students = Json.stringify (students)//Converts the JSON object to a string

localstorage.setitem ("Students", students); Save the converted string with Localstorage
The JSON object is saved above, and then we retrieve the
 
var students = Localstorage.getitem ("Students") with the stored students variable when we want to use it;//Retrieve the students variable
 
students = json.parse (students);//Convert String to JSON object
 
//above to get stored students JSON object
Utils = {Setparam:function (name,value) {Localstorage.setitem (Name,value)}, Getparam:function (NA
Me) {return Localstorage.getitem (name)}} product={id:0, Name: "", num:0, price:0.00}; 
orderdetail={Username: "", Phone: "", Address: "", ZipCode: "", Totalnumber:0, totalamount:0.00}
        Cart = {//Add merchandise to cart addproduct:function (product) {var ShoppingCart = Utils.getparam ("ShoppingCart"); if (ShoppingCart = null | | ShoppingCart = = "") {//The first time to add the product var jsonstr = {"ProductList": [{"id": product.id, "name": Produc T.name, "num": Product.num, "Price": Product.price}], "Totalnumber": Product.num, "TotalAmount": (Product.price *
            Product.num)};
        Utils.setparam ("ShoppingCart", "'" + json.stringify (JSONSTR));
            else {var jsonstr = Json.parse (Shoppingcart.substr (1, shoppingcart.length));
  var productlist = jsonstr.productlist;          var result = false;
                    Find out if the item is in the cart (var i in productlist) {if (productlist[i].id = = product.id) {
                    Productlist[i].num = parseint (productlist[i].num) + parseint (product.num);
                result = true; } if (!result) {//without the product, add directly to Productlist.push ({"id": Product.
            ID, "name": Product.name, "num": Product.num, "Price": Product.price});
            //Recalculate the total price jsonstr.totalnumber = parseint (jsonstr.totalnumber) + parseint (product.num);
            Jsonstr.totalamount = parsefloat (jsonstr.totalamount) + (parseint (product.num) * parsefloat (Product.price));
            Orderdetail.totalnumber = Jsonstr.totalnumber;
            Orderdetail.totalamount = Jsonstr.totalamount;
        Save Cart Utils.setparam ("ShoppingCart", "'" + json.stringify (JSONSTR)); },//Modify the number of items to buy UpdateproducTnum:function (ID, num) {var shoppingcart = Utils.getparam ("ShoppingCart");
        var jsonstr = Json.parse (Shoppingcart.substr (1, shoppingcart.length));

        var productlist = jsonstr.productlist; for (var i in productlist) {if (productlist[i].id = = id) {Jsonstr.totalnumber = parseint (jso
                Nstr.totalnumber) + (parseint (num)-parseint (Productlist[i].num)); Jsonstr.totalamount = parsefloat (Jsonstr.totalamount) + ((parseint (NUM) * parsefloat (Productlist[i].price))-ParseInt
                (productlist[i].num) * parsefloat (Productlist[i].price));
                Productlist[i].num = parseint (num);
                Orderdetail.totalnumber = Jsonstr.totalnumber;
                Orderdetail.totalamount = Jsonstr.totalamount;
                Utils.setparam ("ShoppingCart", "'" + json.stringify (JSONSTR));
            Return //Get all the merchandise in the cart getproductlist:function () {var ShoppingCart = Utils.GetParam ("ShoppingCart");
        var jsonstr = Json.parse (Shoppingcart.substr (1, shoppingcart.length));
        var productlist = jsonstr.productlist;
        Orderdetail.totalnumber = Jsonstr.totalnumber;
        Orderdetail.totalamount = Jsonstr.totalamount;
    return productlist;
        //Determine if there is a commodity existproduct:function (ID) {var result = False in the shopping cart;
        var ShoppingCart = Utils.getparam ("ShoppingCart");
            if (ShoppingCart!= null) {var jsonstr = Json.parse (Shoppingcart.substr (1, shoppingcart.length));
            var productlist = jsonstr.productlist;
                for (var i-productlist) {if (productlist[i].id = = id) {result = true;
    }} return result;
        Deleteproduct:function (ID) {var shoppingcart = Utils.getparam ("ShoppingCart") of the goods in the shopping cart;
        var jsonstr = Json.parse (Shoppingcart.substr (1, shoppingcart.length)); VaR productlist = jsonstr.productlist;
        var list = []; for (var i in productlist) {if (productlist[i].id = = id) {Jsonstr.totalnumber = parseint (jso
                Nstr.totalnumber)-parseint (Productlist[i].num); Jsonstr.totalamount = parsefloat (jsonstr.totalamount)-parseint (productlist[i].num) * parsefloat (productlist[i).
            Price);
            else {List.push (productlist[i]);
        } jsonstr.productlist = list;
        Orderdetail.totalnumber = Jsonstr.totalnumber;
        Orderdetail.totalamount = Jsonstr.totalamount;
    Utils.setparam ("ShoppingCart", "'" + json.stringify (JSONSTR)); }
};
Above based on HTML5 localstorage shopping Cart JS script uses method
var product =
{
	' id ': ID,        //property name is enclosed in quotes, and attributes are separated by commas
	' name ': ' HHH ',
	' num ': JQ (' #text-4 '). Val (),
	' price ': 199.9
};
Products added to the shopping cart
cart.addproduct (product);
var productlist=cart.getproductlist ()//Take out the shopping cart product
alert (', ' Goods: ' +productlist[0].id+ ' +productlist[0].name+ ') +productlist[0].num+ ' +productlist[0].price, ' OK ');


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.