javascript结合Cookies实现浏览记录历史
2010-09-14 13:29:18 来源:WEB开发网运行可以看到效果
<html>
<body>
<script>
//cookie的相关函数
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function setCookie (name, value) {
var exp = new Date();
exp.setTime (exp.getTime()+3600000000);
document.cookie = name + "=" + value + "; expires=" + exp.toGMTString();
}
////////////////////////////////////
function glog(evt)
{
evt=evt?evt:window.event;var srcElem=(evt.target)?evt.target:evt.srcElement;
try
{
while(srcElem.parentNode&&srcElem!=srcElem.parentNode)
{
if(srcElem.tagName&&srcElem.tagName.toUpperCase()=="A")
{
linkname=srcElem.innerHTML;
address=srcElem.href+"_www.achome.cn_";
wlink=linkname+"+"+address;
old_info=getCookie("history_info");
var insert=true;
////////////////////////
if(old_info==null) //判断cookie是否为空
{
insert=true;
}
else
{
var old_link=old_info.split("_www.achome.cn_");
for(var j=0;j<=5;j++)
{
if(old_link[j].indexOf(linkname)!=-1)
insert=false;
if(old_link[j]=="null")
break;
}
}
/////////////////////////////
if(insert) //如果符合条件则重新写入数据
{
wlink+=getCookie("history_info");
setCookie("history_info",wlink);
history_show().reload();
break;
}
}
srcElem = srcElem.parentNode;
}
}
catch(e){}
return true;
}
document.onclick=glog;
////////////////////////////////////////////////////////////////////////////////
function history_show()
{
var history_info=getCookie("history_info");
var content="";
if(history_info!=null)
{
history_arg=history_info.split("_www.achome.cn_");
var i;
for(i=0;i<=5;i++)
{
if(history_arg[i]!="null")
{
var wlink=history_arg[i].split("+");
content+=("<font color='#ff000'>↑</font>"+"<a href='"+wlink[1]+"' target='_blank'>"+wlink[0]+"</a>
");
}
document.getElementById("history").innerHTML=content;
}
}
else
{document.getElementById("history").innerHTML="对不起,您没有任何浏览纪录";}
}
// JavaScript Document
</script>
<div>浏览历史排行(只显示6个最近访问站点并且没有重复的站点出现)</div>
<div id="history">
<script>
history_show();
</script>
</div>
<div>
点击链接:
<a href="http://j5c.ddvip.com/index.php#">网站1</a>
<a href="http://j5c.ddvip.com/index.php#">网站2</a>
<a href="http://j5c.ddvip.com/index.php#">网站3</a>
<a href="http://j5c.ddvip.com/index.php#">网站4</a>
<a href="http://j5c.ddvip.com/index.php#">网站5</a>
<a href="http://j5c.ddvip.com/index.php#">网站6</a>
<a href="http://j5c.ddvip.com/index.php#">网站7</a>
<a href="http://j5c.ddvip.com/index.php#">网站8</a>
<a href="http://j5c.ddvip.com/index.php#">网站9</a>
</div>
<div>如果有其他疑问请登陆<a href=http://www.jb51.net>脚本之家</a>与我联系</div>
</body>
</html>
Tags:javascript 结合 Cookies
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接