cookie 最近瀏覽記錄(中文escape轉(zhuǎn)碼)具體實現(xiàn)
更新時間:2013年06月08日 11:23:07 作者:
cookie 最近瀏覽記錄(中文escape轉(zhuǎn)碼)具體實現(xiàn),需要的朋友可以參考一下
[javascript]
/**
*pro_id: 產(chǎn)品id
*brand_name : 品牌
*pro_name : 產(chǎn)品id
*price : 價格
* catalog : 機型
* img : 圖片
*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); //添加一個新的記錄
setCookie('history',json.toString(),30);
}
}
//顯示歷史記錄
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]['catalog'],json['history'][i]['img'],json['history'][i]['file_name'],"history");
displayNum--;
if(displayNum==0){break;}
}
}
//添加一個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("<img src='"+unescape(img)+"' onerror="+"this.src='/images/nopic.gif'"+" width='90' height='50' /><h3> "+unescape(brand_name+pro_name+catalog)+"</h3><span class='price'>"+unescape(price)+"</span>") ;
var li=document.createElement('li');
li.appendChild(a);
jQuery(li).appendTo("#"+pid) ;
}
//添加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;
}
//獲取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 ""
}
// 清除cookie
function DeleteCookie(){
setCookie('history',"{history:[]}",30);
}
json.js
[javascript]
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 object\],/ig,'').replace(/,\}/g,'}').replace(/,\]/g,']').slice(0,-1);
},
push:function(sName,sValue){
this.self[sName] = sValue;
this[sName] = sValue;
}
}
加一個cookie
addHistory("<%=uuid%>",escape("<%=factoryname%>"),escape("<%=name%>"),escape("<%=price%>"),escape("<%=catalogname%>"),escape("<%=img%>"),escape("<%=file_name%>"));
復制代碼 代碼如下:
/**
*pro_id: 產(chǎn)品id
*brand_name : 品牌
*pro_name : 產(chǎn)品id
*price : 價格
* catalog : 機型
* img : 圖片
*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); //添加一個新的記錄
setCookie('history',json.toString(),30);
}
}
//顯示歷史記錄
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]['catalog'],json['history'][i]['img'],json['history'][i]['file_name'],"history");
displayNum--;
if(displayNum==0){break;}
}
}
//添加一個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("<img src='"+unescape(img)+"' onerror="+"this.src='/images/nopic.gif'"+" width='90' height='50' /><h3> "+unescape(brand_name+pro_name+catalog)+"</h3><span class='price'>"+unescape(price)+"</span>") ;
var li=document.createElement('li');
li.appendChild(a);
jQuery(li).appendTo("#"+pid) ;
}
//添加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;
}
//獲取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 ""
}
// 清除cookie
function DeleteCookie(){
setCookie('history',"{history:[]}",30);
}
json.js
[javascript]
復制代碼 代碼如下:
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 object\],/ig,'').replace(/,\}/g,'}').replace(/,\]/g,']').slice(0,-1);
},
push:function(sName,sValue){
this.self[sName] = sValue;
this[sName] = sValue;
}
}
加一個cookie
復制代碼 代碼如下:
addHistory("<%=uuid%>",escape("<%=factoryname%>"),escape("<%=name%>"),escape("<%=price%>"),escape("<%=catalogname%>"),escape("<%=img%>"),escape("<%=file_name%>"));
相關文章
用javascript獲取當頁面上鼠標光標位置和觸發(fā)事件的對象的代碼
用 javascript 獲取當頁面上鼠標(光標)位置 和 觸發(fā)事件的對象 的方法2009-12-12
Javascript 靜態(tài)頁面實現(xiàn)隨機顯示廣告的辦法
最近在做私服發(fā)布站時,客戶要求實現(xiàn)廣告隨機排序,而且要求在html頁面實現(xiàn),也就是說必須使用javascript來完成了。2010-11-11
Three.js中如何使用CSS3DRenderer和CSS3DSprite實現(xiàn)模型標簽文字
在Three.js中,使用CSS3DRenderer和CSS3DSprite可以輕松地實現(xiàn)模型標簽文字的效果,為場景中的模型提供更直觀的信息展示,本文將介紹如何使用這兩個工具來實現(xiàn)模型標簽文字,并提供相應的代碼示例,感興趣的朋友跟隨小編一起看看吧2024-05-05
js傳遞數(shù)組參數(shù)到后臺controller的方法
下面小編就為大家分享一篇js傳遞數(shù)組參數(shù)到后臺controller的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
用DIV完美模擬createPopup 彈出窗口(腳本之家修正版),支持Firefox,ie,chrome
最近要重構公司的一個站,有一個拾色器只支持IE,不支持FIREFOX CHROME等瀏覽器,花了點時間對照原來的重寫了個。完美實現(xiàn)createPopup方法的彈窗效果,歡迎大家拍磚!2009-09-09

