Home  >  Article  >  php教程  >  js 历史记录 html5版本

js 历史记录 html5版本

WBOY
WBOYOriginal
2016-06-07 11:41:55950browse

之前有个 cookie版本的 历史记录工具,现在给个html5版本的
还是原来的用法,还是熟悉的风格。 不知有没有bug,自己测试了一下没发现
cookie版本

http://www.thinkphp.cn/code/638.html


/**<br>  * History html5 version<br>  * @author yulipu<br>  * <br>  *     用法<br>  *      var his = new History_Html5('key');  // 参数为键值<br>  *         his.add("标题", "连接 如 www.baidu.com", "其他内容"); <br>  *         得到历史数据 返回的是json数据<br>  *         var data = his.getList();  // [{title:"标题", link:"www.baidu.com", other:"其他内容"}]<br>  *     删除记录<br>  *         his.clearHistory();<br>  */<br> function History_Html5(key) {<br>     this.local = window.localStorage;<br>     this.limit = 10;  // 最多10条记录<br>     this.key = key || 'y_his';  // 键值<br>     this.jsonData = null;  // 数据缓存<br> }<br> History_Html5.prototype = {<br>     constructor : History_Html5<br>     ,getLocalData : function(key) {<br>         var d = this.local.getItem(key);<br>         <br>         return null == d ? null : decodeURIComponent(d);<br>     }<br>     ,setLocalData : function(key, value) {<br>         this.local.setItem(key, encodeURIComponent(value));<br>     }<br>     ,delLocalData : function(key) {<br>         this.local.removeItem(key);<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.getLocalData(this.key);<br> <br>         // 有数据<br>         if(null != jsonStr && "" != 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>             // 没有数据<br>             jsonStr = '['+ this.initRow(title, link, other) +']';<br>         }<br>         <br>         this.jsonData = this.parse2Json(jsonStr);<br>         this.delLocalData(this.key);  // 一些浏览器有 bug 先删除再添加 <br>         this.setLocalData(this.key, jsonStr);<br>     }<br>     // 得到记录<br>     ,getList : function() {<br>         // 有缓存直接返回<br>         if(null != this.jsonData) {<br>             return this.jsonData;  // Array<br>         } <br>         // 没有缓存从 localStorage 取<br>         var jsonStr = this.getLocalData(this.key);<br>         if(null != jsonStr && "" != jsonStr) {<br>             this.jsonData = this.parse2Json(jsonStr);<br>         }<br>         <br>         return this.jsonData;<br>     }<br>     // 清空历史<br>     ,clearHistory : function() {<br>         this.delLocalData(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