JS基于cookie實現(xiàn)來賓統(tǒng)計記錄訪客信息的方法
更新時間:2015年08月04日 17:41:06 作者:皮蛋
這篇文章主要介紹了JS基于cookie實現(xiàn)來賓統(tǒng)計記錄訪客信息的方法,通過javascript記錄訪客信息到cookie的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JS基于cookie實現(xiàn)來賓統(tǒng)計記錄訪客信息的方法。分享給大家供大家參考。具體如下:
這里使用JavaScript記錄訪客的來賓信息,記錄是第幾次來訪,顯示的信息有:您的名字;您瀏覽該網(wǎng)頁的次數(shù);您上次瀏覽網(wǎng)頁的時間。可以更改姓名。
運行效果如下圖所示:

具體代碼如下:
<html>
<head>
<title>記錄客戶信息</title>
<script language="JavaScript">
<!--
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function Who(info){
var VisitorName = GetCookie('VisitorName')
if (VisitorName == null) {
VisitorName = prompt("請輸入姓名:",'');
SetCookie ('VisitorName', VisitorName, exp);
}
return VisitorName;
}
function When(info){
var rightNow = new Date()
var WWHTime = 0;
WWHTime = GetCookie('WWhenH')
WWHTime = WWHTime * 1
var lastHereFormatting = new Date(WWHTime); // 訪問時間記錄r
var intLastVisit = (lastHereFormatting.getYear() * 10000)+(lastHereFormatting.getMonth() * 100) + lastHereFormatting.getDate()
var lastHereInDateFormat = "" + lastHereFormatting;
var dayOfWeek = lastHereInDateFormat.substring(0,3)
var dateMonth = lastHereInDateFormat.substring(4,11)
var timeOfDay = lastHereInDateFormat.substring(11,16)
var year = lastHereInDateFormat.substring(23,25)
var WWHText = dayOfWeek + ", " + dateMonth + " at " + timeOfDay // display
SetCookie ("WWhenH", rightNow.getTime(), exp)
return WWHText;
}
function Count(info){
var WWHCount = GetCookie('WWHCount')
if (WWHCount == null) {
WWHCount = 0;
}
else{
WWHCount++;
}
SetCookie ('WWHCount', WWHCount, exp);
return WWHCount;
}
function set(){
VisitorName = prompt("??????",'');
SetCookie ('VisitorName', VisitorName, exp);
SetCookie ('WWHCount', 0, exp);
SetCookie ('WWhenH', 0, exp);
}
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) { //讀取cookie中的信息
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) //設(shè)置Cookie內(nèi)容
{
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
// 歷史記錄
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
-->
</script>
</head>
<body bgcolor="#ADADAD">
<hr>
<div align="center"><center>
<font color="#FF0000">訪客登記</font></p>
<table border="0" width="523" cellspacing="0" cellpadding="0" height="34">
<tr>
<td width="1" height="16"><br>
</td>
<td width="552" height="16"><table border="1" width="100%">
<tr>
<td width="100%" bgcolor="#e8ffe8"><font color="#CC0099"><script language="JavaScript">
<!-- Hide this script from old browsers --
document.write("您好 <b>" + Who() + "</b>,您是第 <b>" + Count() + "</b> 次訪問本主頁. 最后一次是 <b>" + When() +"</b>. ")
document.write('<a href="JavaScript:set()">[按此改名]</a>')
// -- End Hiding Here -->
</script>
</font></td>
</tr>
</table>
</center></div>
<div align="center">
<font class=30pt>顯示的信息有:您留的名字;您瀏覽該網(wǎng)頁的次數(shù);您上次瀏覽網(wǎng)頁的時間??梢愿男彰?。
</font>
</div>
</body>
</html>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
JavaScript動態(tài)檢測密碼強度原理及實現(xiàn)方法詳解
這篇文章主要介紹了JavaScript動態(tài)檢測密碼強度原理及實現(xiàn)方法,結(jié)合具體實例形式詳細分析了javascript針對輸入字符串密碼強度檢測的原理與相關(guān)判斷操作技巧,需要的朋友可以參考下2019-06-06
基于JS實現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動條滾動
這篇文章主要介紹了基于JS實現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動條滾動,代碼簡單易懂非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
JavaScript必看的10道面試題總結(jié)(推薦)
JavaScript 已經(jīng)成為全棧開發(fā)技能的基石,在全棧開發(fā)面試中都會不可避免地涉及到與 JavaScript 有關(guān)的問題。這篇文章主要給大家介紹了關(guān)于JavaScript必看的10道面試題,需要的朋友可以參考下2021-05-05

