欢迎您访问我爱IT技术网,今天小编为你分享的javascript教程:【cookie 最近浏览记录(中文escape转码)具体实现】,下面是详细的讲解!
cookie 最近浏览记录(中文escape转码)具体实现
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='http://www.jb51.net/proDetail/'+file_name;
a.setAttribute('href',href);
a.setAttribute("target","_blank") ;
jQuery(a).html("<img src='http://www.jb51.net/article/"+unescape(img)+"' onerror="+"this.src='http://www.jb51.net/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);
}
关于cookie 最近浏览记录(中文escape转码)具体实现的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【firefox】firefox浏览器不支持innerText的解决
- 【Extjs】Extjs学习过程中新手容易碰到的低级错误
- 【clearInterval】js clearInterval()方法的定义
- 【ComboBox】ComboBox 和 DateField 在IE下消失的
- 【dom】javascript dom追加内容实现示例-追加内容
- 【has】基于jquery的has()方法以及与find()方法以
- 【extjs】Extjs入门之动态加载树代码-动态加载树
- 【checkbox】让checkbox不选中即将选中的checkbox
- 【Array】js中更短的 Array 类型转换-类型转换
- 【append】append和appendTo的区别以及appendChil
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
