一个实现记录浏览历史的javascript工具
此版本有bug 建议用一下这个
http://www.thinkphp.cn/code/638.html/**<br>
* History<br>
* 用法<br>
* var his = new History('key'); // 参数标示cookie的键值<br>
* his.add("标题", "连接 比如 http://www.baidu.com", "其他内容"); <br>
* 得到历史数据 返回的是json数据<br>
* var data = his.getList(); // [{title:"标题", "link": "http://www.baidu.com"}]<br>
* <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>
}<br>
History.prototype = {<br>
constructor : History<br>
,addCookie: function(name, value, expiresHours, options) {<br>
options = options || {};<br>
var cookieString = name + "=" + escape(value); <br>
//判断是否设置过期时间 <br>
if(undefined != expiresHours && expiresHours > 0){ <br>
var date = new Date(); <br>
date.setTime(date.getTime() + expiresHours * 3600 * 1000); <br>
cookieString = cookieString + "; expires=" + date.toUTCString(); <br>
} <br>
var path = options.path ? '; path=' + options.path : '',<br>
domain = options.domain ? '; domain=' + options.domain : '',<br>
secure = options.secure ? '; secure' : '';<br>
<br>
//alert(cookieString + path + domain + secure);<br>
document.cookie = cookieString + path + domain + secure; <br>
}<br>
,getCookie: function(name) { <br>
var cookies = document.cookie, //得到本域下的所有cookie -- "userId=828; userName=lisi"<br>
arrCookie = cookies.split(";"),<br>
val = "",<br>
tmpArr = "";<br>
for(var i=0; i<arrcookie.length></arrcookie.length>
tmpArr = arrCookie[i].split("="); <br>
if(tmpArr[0] == name) {<br>
val = unescape(tmpArr[1]);<br>
break;<br>
}<br>
}<br>
return val.toString(); <br>
}<br>
,deleteCookie: function(name) {<br>
var date = new Date(); <br>
date.setTime(date.getTime()-10000); <br>
document.cookie = name + "=''; expires="+date.toUTCString(); <br>
}<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>
this.jsonData = this.parse2Json(jsonStr);<br>
this.addCookie(this.key, jsonStr, this.cacheTime);<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>
};
His.rar ( 1.43 KB 下载:84 次 )
AD:真正免费,域名+虚机+企业邮箱=0元