這個計數器的關鍵是 Cookie 技術與動態圖像特性的結合。
Cookie 能在使用者端的硬碟上記錄資料,下次造訪同一站點時即可讀回,直接得知來訪者的身份與訪問次數。JavaScript 透過 document.cookie 屬性存取 Cookie,這個屬性包含名稱、失效日期、有效網域、有效路徑等資訊;以等號分隔的名稱與值就是 Cookie 的實際資料,本例用來存放該訪客的累積造訪次數。
顯示部分則利用「把網頁中的圖片對應到 images 陣列」的特性:預先載入一組數字圖片,每次呼叫時依次數決定要顯示哪幾張,藉由覆寫 img.src 達成動態更新。
值得注意的是,這不同於一般意義上的「網站總瀏覽次數」,而是記錄單一訪客造訪該頁的次數。
< html> < head> < meta http-equiv=″Content-Type″ content=″text/html; charset=gb2312″> < title>趣味計數器< /title> < /head> < body> < p>< script language=″JavaScript″> var expdays=60; var exp=new Date(); exp.setTime(exp.getTime() (expdays*24*60*60*1000)); function count(info){ //若是該訪客的第一次訪問,將計數器值賦1,否則加1累積 var wwhcount=getcookie(′wwhcount′); if (wwhcount==null){ wwhcount=1; } else{wwhcount++;} setcookie(′wwhcount′,wwhcount,exp); return countdisp(wwhcount) } function countdisp(countvar){ //實現隨機顯示,不足6位以0補全,可以自己調整顯示位數 var countvar1=″000000″+countvar; var howFar1=countvar1.length; countvar1=countvar1.substring(howFar1, howFar1-1) var index=″ ″+Math.floor(Math.random()*10); if (index==″10″){ index=″0″}; for (var icount=0;icount< 6;icount++){ var g=countvar1.substring(icount,icount+1); document.images[icount].src=″http: //localhost/images/″+index+g+″.gif″; } } 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中的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 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>< /p> < ! --預載入圖像數組--> 您是第 < img src=″https://localhost/images/00.gif″ height=20 width=20> < img src=″https://localhost/images/00.gif″ height=20 width=20> < img src=″https://localhost/images/00.gif″ height=20 width=20> < img src=″https://localhost/images/00.gif″ height=20 width=20> < I mg src=″https://localhost/images/00.gif″ height=20 width=20> < img src=″https://localhost/images/00.gif″ height=20 width=20>次光臨! < script language=″JavaScript″> //調用count()函數,實現計數器的動態圖像顯示 count(); < /script> < /body> < /html>
getcookie(name) 負責在 document.cookie 這個用分號串接的長字串中,找出指定名稱的片段並取出其值。
setcookie(name, value, ...) 負責寫入,並可選地指定 expires、path、domain、secure。把 expires 設為 60 天後,Cookie 就會在兩個月後自動失效。
deletecookie(name) 的作法是「把失效時間設為過去」,這是刪除 Cookie 的標準手法——Cookie 本身沒有 delete 方法。
countdisp() 負責把次數補足六位(例如 7 變成 000007),再逐位換成對應的數字圖片。
這份程式碼示範的觀念仍然有效,但實作方式建議大幅現代化:
用 document.cookie 手刻字串處理太脆弱。 現在一律改用 localStorage.getItem/setItem,API 乾淨得多,也不會有 escape/unescape 的編碼問題(這兩個函式已從標準中移除)。
請勿用 Cookie 做真正的流量統計。 使用者清除 Cookie、切換瀏覽器、使用無痕模式都會讓數字歸零或重複計算。真正的統計必須在伺服器端以 log 或分析工具為之,Cookie 只能做「粗略的個人化」。
涉及個資要注意法規。 在歐盟等地,非必要的 Cookie 需要先取得使用者同意(cookie consent),這在多年前寫這段程式時還不是問題,現在則是上線前必須處理的一環。
為什麼用圖片而不是文字? 當年這麼做是為了美觀(自訂數字字型)與某種程度的防偽。今天直接用 CSS 指定字型即可,圖片方案反而帶來額外請求與無障礙問題——顯示次數的容器記得加上適當的 aria-label,讓螢幕閱讀器能唸出「您是第 N 位訪客」。