JS基于cookie實(shí)現(xiàn)來賓統(tǒng)計(jì)記錄訪客信息的方法
本文實(shí)例講述了JS基于cookie實(shí)現(xiàn)來賓統(tǒng)計(jì)記錄訪客信息的方法。分享給大家供大家參考。具體如下:
這里使用JavaScript記錄訪客的來賓信息,記錄是第幾次來訪,顯示的信息有:您的名字;您瀏覽該網(wǎng)頁的次數(shù);您上次瀏覽網(wǎng)頁的時(shí)間??梢愿男彰?。
運(yùn)行效果如下圖所示:
具體代碼如下:
<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("請(qǐng)輸入姓名:",''); 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); // 訪問時(shí)間記錄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)頁的時(shí)間。可以更改姓名。 </font> </div> </body> </html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- JavaScript 利用Cookie記錄用戶登錄信息
- JS讀取cookies信息(記錄用戶名)
- 靜態(tài)頁面利用JS讀取cookies記住用戶信息
- 只出現(xiàn)一次的提示信息(js+cookies)
- JavaScript使用cookie記錄臨時(shí)訪客信息的方法
- JS設(shè)置cookie、讀取cookie、刪除cookie
- JS操作Cookies包括(讀取添加與刪除)
- js讀取cookie方法總結(jié)
- javascript創(chuàng)建cookie、讀取cookie
- javascript cookies 設(shè)置、讀取、刪除實(shí)例代碼
- JS使用cookie保存用戶登錄信息操作示例
相關(guān)文章
JavaScript動(dòng)態(tài)檢測(cè)密碼強(qiáng)度原理及實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JavaScript動(dòng)態(tài)檢測(cè)密碼強(qiáng)度原理及實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了javascript針對(duì)輸入字符串密碼強(qiáng)度檢測(cè)的原理與相關(guān)判斷操作技巧,需要的朋友可以參考下2019-06-06JavaScript實(shí)現(xiàn)一個(gè)空中避難的小游戲
最近利用Javascript實(shí)現(xiàn)了一個(gè)小游戲,覺著還不錯(cuò),所以分享給大家,下面這篇文章主要給大家介紹了利用JavaScript實(shí)現(xiàn)一個(gè)空中避難的小游戲的相關(guān)資料,文中給出了完整的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-06-06基于JS實(shí)現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動(dòng)條滾動(dòng)
這篇文章主要介紹了基于JS實(shí)現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動(dòng)條滾動(dòng),代碼簡(jiǎn)單易懂非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02JavaScript必看的10道面試題總結(jié)(推薦)
JavaScript 已經(jīng)成為全棧開發(fā)技能的基石,在全棧開發(fā)面試中都會(huì)不可避免地涉及到與 JavaScript 有關(guān)的問題。這篇文章主要給大家介紹了關(guān)于JavaScript必看的10道面試題,需要的朋友可以參考下2021-05-05