首页 >

js操作cookie保存浏览记录办法【javascript】

web前端|js教程js操作cookie保存浏览记录办法【javascript】
js,cookie,保存,浏览记录
web前端-js教程
微信商城平台 源码,ubuntu无法获取锁,tomcat配置关闭端口号,sf轻小说爬虫,北大青鸟可以学php吗,自学seo基础lzw
说明:最近做了一个功能,记录用户浏览过的产品页面。我的思路是,客户每次进入产品页面,就自己调用JS把产品信息以json的形式保存到cookie里面。
展会源码,xp仿ubuntu主题,tomcat怎样不缓存js,php网页 爬虫,php 检查敏感词,三水公司seo优化多少钱lzw
浏览记录的显示是从cookie里读出来,然后解析成json,生成html元素。因为用户可能会同时打开好几个页面,这几个页面上可能都有浏览记录,为了使即使显示浏览记录,每秒中刷新一次。
红包返现源码,ubuntu查看安装服务,c 爬虫获取时间,__set() php,seo优化指数lzw
要用到2个js文件,history.js,关键的聊天记录保存和读取代码。json.js,对json进行处理。

history.js

var addHistory=function(num,id){  stringCookie=getCookie('history');  var stringHistory=""!=stringCookie?stringCookie:"{history:[]}";  var json=new JSON(stringHistory);  var e="{num:"+num+",id:"+id+"}";  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]['num'],json['history'][i]['id'],"history");     displayNum--;    if(displayNum==0){break;}  }}//添加一个li元素var addLi=function(num,id,pid){  var a=document.createElement('a');  var href='product.action?pid='+id;  a.setAttribute('href',href);  var t=document.createTextNode(num);  a.appendChild(t);  var li=document.createElement('li');  li.appendChild(a);  document.getElementById(pid).appendChild(li);}//添加cookievar setCookie=function(c_name,value,expiredays){  var exdate=new Date()  exdate.setDate(exdate.getDate()+expiredays)  cookieVal=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());//  alert(cookieVal);  document.cookie=cookieVal;}//获取cookiefunction 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//    document.write(document.cookie.substring(c_start,c_end)+"
"); return unescape(document.cookie.substring(c_start,c_end)) } } return ""}
json.js

var JSON = function(sJSON){  this.objType = (typeof sJSON);  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(/objectobject,/ig,'').replace(/,\}/g,'}').replace(/,\]/g,']').slice(0,-1);  },  push:function(sName,sValue){    this.self[sName] = sValue;    this[sName] = sValue;  }}

js操作cookie保存浏览记录办法【javascript】
  • 使用Cookies保存网站历史浏览记录实例代码【javascript】
  • 使用Cookies保存网站历史浏览记录实例代码【javascript】 | 使用Cookies保存网站历史浏览记录实例代码【javascript】 ...

    js操作cookie保存浏览记录办法【javascript】
  • 网页浏览记录删除后还可以查看吗
  • 网页浏览记录删除后还可以查看吗 | 网页浏览记录删除后还可以查看吗 ...