Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-06-07 11:42:04
Original
1125 Leute haben es durchsucht

前一个版本存在删除历史记录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元

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!