船长 发表于 2009-03-21 11:17 | 分类:javascript | 阅读数:408 次
显示最后一次访问时间的js脚本,如果是第一次访问,会显示一条欢迎信息,如果是回头客,则会显示上次访问的时间,利用cookie实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>显示最后一次访问时间</title> </head> <body> <br> <br> <div style="color:red">*刷新页面查看时间变化</div> <script type = "text/javascript"> var days = 730; // days until cookie expires = 2 years. var lastvisit=new Object(); var firstvisitmsg="这是你第一次访问此页面. 欢迎!"; lastvisit.subsequentvisitmsg="欢迎回来! 你上一次访问是在: <b>[displaydate]</b>"; lastvisit.getCookie=function(Name){ var re=new RegExp(Name+"=[^;]+", "i"); if (document.cookie.match(re)) return document.cookie.match(re)[0].split("=")[1]; return''; } lastvisit.setCookie=function(name, value, days){ var expireDate = new Date(); var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days)); document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"; } lastvisit.showmessage = function() { var wh = new Date(); if (lastvisit.getCookie("visitc") == "") { lastvisit.setCookie("visitc", wh, days); document.write(firstvisitmsg); } else { var lv = lastvisit.getCookie("visitc"); var lvp = Date.parse(lv); var now = new Date(); now.setTime(lvp); var day = new Array("星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"); var month = new Array ("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"); var dd = now.getDate(); var dy = now.getDay(); dy = day[dy]; var mn = now.getMonth(); mn = month[mn]; yy = now.getFullYear(); var hh = now.getHours(); var ampm = "上午"; if (hh >= 12) {ampm = "下午"} if (hh >12){hh = hh - 12}; if (hh == 0) {hh = 12} if (hh < 10) {hh = "0" + hh}; var mins = now.getMinutes(); if (mins < 10) {mins = "0"+ mins} var secs = now.getSeconds(); if (secs < 10) {secs = "0" + secs} var dispDate = yy + "年" + mn + dd + "日," + dy + " ," + ampm + " " + hh + ":" + mins + ":" + secs//dy + ", " + mn + " " + dd + ", " + yy + " " + hh + ":" + mins + ":" + secs + " " + ampm document.write(lastvisit.subsequentvisitmsg.replace("\[displaydate\]", dispDate)) } lastvisit.setCookie("visitc", wh, days); } lastvisit.showmessage(); </script> <br> 代码整理:<a href="http://www.songlecn.com">web集结号-http://www.songlecn.com</a> <body></html>
提示:你可以先修改部分代码再运行。
您的网名: * 必填
电子邮件: * 绝不会泄露
个人主页:
评论内容:
(Ctrl+Enter快捷回复)
web集结号致力于