Home >php教程 >php手册 >js 记录用户浏览历史记录 Bug 修复版本

js 记录用户浏览历史记录 Bug 修复版本

WBOY
WBOYOriginal
2016-06-07 11:42:041181browse

前一个版本存在删除历史记录bug问题,特此修复.用法不变
http://www.thinkphp.cn/code/513.html

另外提供html5版
http://www.thinkphp.cn/code/669.html
/**<br>  * History<br>  * @author yulipu<br>  */<br> function History(key) {<br>     this.limit = 10;  // 最多10条记录<br>     this.key = key || 'y_his';  // 键值<br>     this.jsonData = null;  // 数据缓存<br>     this.cacheTime = 24;  // 24 小时<br>     this.path = '/';  // cookie path<br> }<br> History.prototype = {<br>     constructor : History<br>     ,setCookie: function(name, value, expiresHours, options) {<br>         options = options || {};<br>         var cookieString = name + '=' + encodeURIComponent(value);<br>         //判断是否设置过期时间<br>         if(undefined != expiresHours){<br>             var date = new Date();<br>             date.setTime(date.getTime() + expiresHours * 3600 * 1000);<br>             cookieString = cookieString + '; expires=' + date.toUTCString();<br>         }<br>         <br>         var other = [<br>             options.path ? '; path=' + options.path : '',<br>             options.domain ? '; domain=' + options.domain : '',<br>             options.secure ? '; secure' : ''<br>         ].join('');<br>         <br>         document.cookie = cookieString + other; <br>     }<br>     ,getCookie: function(name) {<br>         // cookie 的格式是个个用分号空格分隔<br>         var arrCookie = document.cookie ? document.cookie.split('; ') : [],<br>             val = '',<br>             tmpArr = '';<br>             <br>         for(var i=0; i<arrcookie.length></arrcookie.length>             tmpArr = arrCookie[i].split('=');<br>             tmpArr[0] = tmpArr[0].replace(' ', '');  // 去掉空格<br>             if(tmpArr[0] == name) {<br>                 val = decodeURIComponent(tmpArr[1]);<br>                 break;<br>             }<br>         }<br>         return val.toString();<br>     }<br>     ,deleteCookie: function(name) {<br>         this.setCookie(name, '', -1, {"path" : this.path});<br>         //console.log(document.cookie);<br>     }<br>     ,initRow : function(title, link, other) {<br>         return '{"title":"'+title+'", "link":"'+link+'", "other":"'+other+'"}';<br>     }<br>     ,parse2Json : function(jsonStr) {<br>         var json = [];<br>         try {<br>             json = JSON.parse(jsonStr);<br>         } catch(e) {<br>             //alert('parse error');return;<br>             json = eval(jsonStr);<br>         }<br>         <br>         return json;<br>     }<br>     <br>     // 添加记录<br>     ,add : function(title, link, other) {<br>         var jsonStr = this.getCookie(this.key);<br>         //alert(jsonStr); return;<br>         <br>         if("" != jsonStr) {<br>             this.jsonData = this.parse2Json(jsonStr);<br>             <br>             // 排重<br>             for(var x=0; x<this.jsondata.length></this.jsondata.length>                 if(link == this.jsonData[x]['link']) {<br>                     return false;<br>                 }<br>             }<br>             // 重新赋值 组装 json 字符串<br>             jsonStr = '[' + this.initRow(title, link, other) + ',';<br>             for(var i=0; i<this.limit-1></this.limit-1>                 if(undefined != this.jsonData[i]) {<br>                     jsonStr += this.initRow(this.jsonData[i]['title'], this.jsonData[i]['link'], this.jsonData[i]['other']) + ',';<br>                 } else {<br>                     break;<br>                 }<br>             }<br>             jsonStr = jsonStr.substring(0, jsonStr.lastIndexOf(','));<br>             jsonStr += ']';<br>             <br>         } else {<br>             jsonStr = '['+ this.initRow(title, link, other) +']';<br>         }<br>         <br>         //alert(jsonStr);<br>         this.jsonData = this.parse2Json(jsonStr);<br>         this.setCookie(this.key, jsonStr, this.cacheTime, {"path" : this.path});<br>     }<br>     // 得到记录<br>     ,getList : function() {<br>         // 有缓存直接返回<br>         if(null != this.jsonData) {<br>             return this.jsonData;  // Array<br>         } <br>         // 没有缓存从 cookie 取<br>         var jsonStr = this.getCookie(this.key);<br>         if("" != jsonStr) {<br>             this.jsonData = this.parse2Json(jsonStr);<br>         }<br>         <br>         return this.jsonData;<br>     }<br>     // 清空历史<br>     ,clearHistory : function() {<br>         this.deleteCookie(this.key);<br>         this.jsonData = null;<br>     }<br> };

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP邮件发送类【完整版】Next article:………