使用JavaScript製作一個簡單的計數器的方法,javascript計數器
設計思想
該方法的關鍵是Cookie技術和生動影像特性的綜合運用。使用Cookie,可以在使用者端的硬碟上記錄使用者的資料,下次訪問此網站時,即可讀取使用者端硬碟的Cookie,直接得知來訪者的身份和訪問次數等有關資訊。JavaScript中通過document.cookie屬性訪問Cookie,這個屬性包括名字、失效日期、有效網域名稱、有效URL路徑等。用等號分開的名字和其值是Cookie的實際資料,本例中用來儲存該訪問者訪問該頁面的次數。通過把Web頁中的影像地圖到一個Images數組,一定條件下修改該數組項的特性,可以實現生動影像顯示。本例中,首先預載入一組映像,每次調用該Web頁時,隨機產生新的一組映像,通過覆蓋原映像實現動態趣味性效果。
來源程式count.html
< 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=″http://localhost/images/00.gif″ height=20 width=20> < img src=″http://localhost/images/00.gif″ height=20 width=20> < img src=″http://localhost/images/00.gif″ height=20 width=20> < img src=″http://localhost/images/00.gif″ height=20 width=20> < I mg src=″http://localhost/images/00.gif″ height=20 width=20> < img src=″http://localhost/images/00.gif″ height=20 width=20>次光臨! < script language=″JavaScript″> //調用count()函數,實現計數器的生動影像顯示 count(); < /script> < /body> < /html>
注意事項
由於使用了JavaScript語言,因此該方法具有與應用平台的無關性,可以適用於Unix、Windows等多種平台。另外,此計數器不同於一般意義上的訪客計數器,專門用於記錄某一訪客對某一網站的訪問次數。