訪問回数(クッキー document.cookie)
<html> <head> <title>訪問回数</title> <script language="JavaScript"> <!-- life = 30; // クッキーの保存日数 count = 0; // 訪問回数 cookie = document.cookie; // クッキー読み込み data = cookie.split("; "); // "; "で分割 for(i = 0 ; i < data.length ; i++) { data2 = data[i].split("="); // "="で分割 if(data2[0] == "count") { count = data2[1]; // 訪問回数 } } today = new Date(); // 今日の日時 time = today.getTime() + (life * 1000 * 60 * 60 * 24); clear = new Date(); // クッキーの有効期限作成 clear.setTime(time); mon = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); week = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); year = clear.getFullYear(); mon = mon[clear.getMonth()]; date = clear.getDate(); hour = clear.getHours(); min = clear.getMinutes(); sec = clear.getSeconds(); day = week[clear.getDay()]; cookie = "count=" + ++count + ";"; // クッキーデータ作成 cookie += "expires="+day+","+date+"-"+mon+"-"+year+" "+hour+":"+min+":"+sec+";"; document.cookie = cookie; // クッキー書き込み //--> </script> </head> <body> <script language="JavaScript"> <!-- document.write(count, "回目の訪問です。"); //--> </script> </body> </html>
〔 実行する 〕