Use $. Cookie () to store and display historical browsing records

Source: Internet
Author: User

Prerequisites: the cookie. js and jquery. js files must be introduced.

1. Create a cookie

1 function createcookikehistory () {2 var canadd = true; 3 var ck = $. cookie (cookie_name1); 4 if ($. cookie (cookie_name1) = NULL) {5 // product details 6 var jsonstr = "[{'productname ':'" + $ ("# txtname "). val () + "', 'picmiddleurl':'" + $ ("# txtimg "). val () + "', 'salprice':'" + $ ("# txtprice "). val () + "', 'itemcode':'" + $ ("# txtcode "). val () + "', 'productcode':'" + $ ("# txtpcode "). val () + "'}]"; 7 $. cookie (cookie_name1, jsonstr, cookieset); // if no cookie exists, set it to 8} 9 else {10 var jsonobj = eval ('+ $. cookie (cookie_name1) + '); // If yes, convert the JSON string to the object 11 12 // cyclically determine whether the product 13 for (var obj in jsonobj) already exists) {14 if (jsonobj [OBJ]. productcode. tostring () = $ ("# txtpcode "). val (). tostring () {15 canadd = false; 16 return false; 17} 18} 19 if (canadd) {20 jsonobj [jsonobj. length] = new object (); 21 jsonobj [jsonobj. length-1]. productname = $ ("# txtname "). val (); 22 jsonobj [jsonobj. length-1]. picmiddleurl = $ ("# txtimg "). val (); 23 jsonobj [jsonobj. length-1]. saleprice = $ ("# txtprice "). val (); 24 jsonobj [jsonobj. length-1]. itemcode = $ ("# txtcode "). val (); 25 jsonobj [jsonobj. length-1]. productcode = $ ("# txtpcode "). val (); 26 $. cookie (cookie_name1, JSON. stringify (jsonobj), cookieset1); // to write coockie JSON, json2.js is required to support 27} 28} 29}

One

2. Read and display the data in the cookie

 1 function getHistoryProduct() { 2             if ($.cookie(COOKIE_NAME1) != null) { 3                 var json = eval('(' + $.cookie(COOKIE_NAME1) + ')'); 4                 var str = ""; 5                 str += "<div class='w152 fl' style='margin-top:15px'></div>"; 6                 str += "<div class='w152 fl'>"; 7                 for (var i = json.length; i > 0; i--) { 8                     str += "<div class='w152 fl mt10'><a  href='/product/" + json[i - 1].ProductCode + ".html' target='_blank' title='" + json[i - 1].ProductName + "'></a></div>"; 9                     str += "<div class='w152 fl fwb tac mt10'>" + json[i-1].ProductName + "</div>";10                     str += "<div class='w152 fl mt10 tac tjjg'>" + json[i-1].ItemCode + "·" + json[i-1].SalePrice + "</div>";11                     if (json.length-i>=4) {12                         i = 0;13                     }14                 }15                 str += "</div>";16                 $("#reProduct").append(str);17             }18         }

Two

 

 

Function createcookikehistory (){
VaR canadd = true;
VaR ck = $. Cookie (cookie_name1 );
If ($. Cookie (cookie_name1) = NULL ){
// Product details
VaR jsonstr = "[{'productname ':'" + $ ("# txtname "). val () + "', 'picmiddleurl':'" + $ ("# txtimg "). val () + "', 'salprice':'" + $ ("# txtprice "). val () + "', 'itemcode':'" + $ ("# txtcode "). val () + "', 'productcode':'" + $ ("# txtpcode "). val () + "'}]";
$. Cookie (cookie_name1, jsonstr, cookieset); // if this cookie does not exist, set it
}
Else {
VaR jsonobj = eval ('+ $. Cookie (cookie_name1) +'); // If yes, convert the JSON string to an object

// Cyclically determine whether the product already exists
For (var obj in jsonobj ){
If (jsonobj [OBJ]. productcode. tostring () =$ ("# txtpcode"). Val (). tostring ()){
Canadd = false;
Return false;
}
}
If (canadd ){
Jsonobj [jsonobj. Length] = new object ();
Jsonobj [jsonobj. Length-1]. productname = $ ("# txtname"). Val ();
Jsonobj [jsonobj. Length-1]. picmiddleurl = $ ("# txtimg"). Val ();
Jsonobj [jsonobj. Length-1]. saleprice = $ ("# txtprice"). Val ();
Jsonobj [jsonobj. Length-1]. itemcode = $ ("# txtcode"). Val ();
Jsonobj [jsonobj. Length-1]. productcode = $ ("# txtpcode"). Val ();
$. Cookie (cookie_name1, JSON. stringify (jsonobj), cookieset1); // json2.js is required to write coockie JSON
}
}
}

Function gethistoryproduct (){
If ($. Cookie (cookie_name1 )! = NULL ){
VaR JSON = eval ('+ $. Cookie (cookie_name1) + ')');
VaR STR = "";
STR + = "<Div class = 'w152 FL 'style = 'margin-top: 15px'> </div> ";
STR + = "<Div class = 'w152 FL '> ";

// For (var obj in JSON ){
// STR + = "<Div class = 'w152 FL mt10'> <a href = '/product/" + JSON [OBJ]. productcode + ". HTML 'target = '_ blank'> </a> </div> ";
// STR + = "<Div class = 'w152 FL fwb TAC mt10'>" + JSON [OBJ]. productname + "</div> ";
// STR + = "<Div class = 'w152 FL mt10 TAC tjjg'>" + JSON [OBJ]. itemcode + "·" + JSON [OBJ]. saleprice + "</div> ";
//}
// For (VAR I = 0; I <JSON. length; I ++ ){
// STR + = "<Div class = 'w152 FL mt10'> <a href = '/product/" + JSON [I]. productcode + ". HTML 'target = '_ blank'> </a> </div> ";
// STR + = "<Div class = 'w152 FL fwb TAC mt10'>" + JSON [I]. productname + "</div> ";
// STR + = "<Div class = 'w152 FL mt10 TAC tjjg'>" + JSON [I]. itemcode + "·" + JSON [I]. saleprice + "</div> ";
// If (I> = 4 ){
// I = JSON. length;
//}
//}

For (VAR I = JSON. length; I> 0; I --){
STR + = "<Div class = 'w152 FL mt10'> <a href = '/product/" + JSON [I-1]. productcode + ". HTML 'target = '_ blank 'title =' "+ JSON [I-1]. productname + "'> </a> </div> ";
STR + = "<Div class = 'w152 FL fwb TAC mt10 '>" + JSON [I-1]. productname + "</div> ";
STR + = "<Div class = 'w152 FL mt10 TAC tjjg'>" + JSON [I-1]. itemcode + "·" + JSON [I-1]. saleprice + "</div> ";
If (JSON. Length-I> = 4 ){
I = 0;
}
}
STR + = "</div> ";
$ ("# Reproduct"). append (STR );
}
}

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.