[Javascript]
Copy codeThe Code is as follows :/**
* Pro_id: Product id
* Brand_name: Brand
* Pro_name: Product id
* Price: price
* Catalog: Model
* Img: Image
* File_name: File Name
*/
Var addHistory = function (pro_id, brand_name, pro_name, price, catalog, img, file_name ){
StringCookie = getCookie ('History ');
Var stringHistory = ""! = StringCookie? StringCookie: "{history: []}";
Var json = new JSON (stringHistory );
Var pro_ids = '';
JQuery. each (json ['History '], function (I, val ){
Pro_ids + = json ['History '] [I]. pro_id + ",";
});
If (jQuery. trim (pro_id) = ''| pro_ids.indexOf (pro_id) =-1 ){
Var e = "{pro_id:" + pro_id + ", brand_name: '" + brand_name + "', pro_name: '" + pro_name + "', price: '"+ price +"', catalog: '"+ catalog +"', img: '"+ img +"', file_name: '"+ file_name + "'}";
Json ['History ']. push (e); // Add a new record
SetCookie ('History ', json. toString (), 30 );
}
}
// Display historical records
Var DisplayHistory = function (){
Var p_ele = document. getElementById ('History ');
While (p_ele.firstChild ){
P_ele.removeChild (p_ele.firstChild );
}
Var historyJSON = getCookie ('History ');
Var json = new JSON (historyJSON );
Var displayNum = 6;
For (I = json ['History ']. length-1; I> = 0; I --){
AddLi (json ['History '] [I] ['Pro _ id'], json ['History'] [I] ['brand _ name'], json ['History '] [I] ['Pro _ name'], json ['History'] [I] ['price'], json ['History '] [I] ['catalo'], json ['History'] [I] ['img '], json ['History '] [I] ['file _ name'], "history ");
DisplayNum --;
If (displayNum = 0) {break ;}
}
}
// Add a cookie
Var addLi = function (pro_id, brand_name, pro_name, price, catalog, img, file_name, pid ){
Var a = document. createElement ('A ');
Var href = '/proDetail/' + file_name;
A. setAttribute ('href ', href );
A. setAttribute ("target", "_ blank ");
JQuery(a).html (" Var li = document. createElement ('lil ');
Li. appendChild ();
JQuery (li). appendTo ("#" + pid );
}
// Add cookie
Var setCookie = function (c_name, value, expiredays)
{
Var exdate = new Date ()
Exdate. setDate (exdate. getDate () + expiredays)
CookieVal = c_name + "=" + escape (value) + (expiredays = null )? "": "; Path =/; expires =" + exdate. toGMTString ());
Document. cookie = cookieVal;
}
// Obtain the cookie
Function getCookie (c_name)
{
If (document. cookie. length> 0)
{
C_start = document. cookie. indexOf (c_name + "= ")
If (c_start! =-1)
{
C_start = c_start + c_name.length + 1
C_end = document. cookie. indexOf (";", c_start)
If (c_end =-1) c_end = document. cookie. length
Return unescape (document. cookie. substring (c_start, c_end ))
}
}
Return ""
}
// Clear cookie
Function DeleteCookie (){
SetCookie ('History ', "{history: []}", 30 );
}
Json. js
[Javascript]Copy codeThe Code is as follows: var JSON = function (sJSON ){
This. objType = (typeof sJSON );
If (this. objType = 'string' & ''= sJSON ){
SJSON = '{history: []}';
}
This. self = [];
(Function (s, o ){
For (var I in o ){
O. hasOwnProperty (I) & (s [I] = o [I], s. self [I] = o [I])
};}) (This, (this. objType = 'string ')? Eval ('0, '+ sJSON): sJSON );
}
JSON. prototype = {
ToString: function (){
Return this. getString ();
},
ValueOf: function (){
Return this. getString ();
},
GetString: function (){
Var sA = [];
(Function (o ){
Var oo = null;
SA. push ('{');
For (var I in o ){
If (o. hasOwnProperty (I) & I! = 'Prototype '){
Oo = o [I];
If (oo instanceof Array ){
SA. push (I + ':[');
For (var B in oo ){
If (oo. hasOwnProperty (B) & B! = 'Prototype '){
SA. push (oo [B] + ',');
If (typeof oo [B] = 'object') arguments. callee (oo [B]);
}
}
SA. push ('],');
Continue;
} Else {
SA. push (I + ': \ ''+ oo + '\',');
}
If (typeof oo = 'object') arguments. callee (oo );
}
}
SA. push ('},');
}) (This. self );
Return sA. slice (0 ). join (''). replace (/\ [object \],/ig ,''). replace (/, \}/g ,'}'). replace (/, \]/g, ']'). slice (0,-1 );
},
Push: function (sName, sValue ){
This. self [sName] = sValue;
This [sName] = sValue;
}
}
Add a cookieCopy codeThe Code is as follows: addHistory ("<% = uuid %>", escape ("<% = factoryname %>"), escape ("<% = name %> "), escape ("<% = price %>"), escape ("<% = catalogname %>"), escape ("<% = img %> "), escape ("<% = file_name %> "));