初回訪問時の日付を表示する
| <html> <head> <script language="JavaScript"> <!-- var strCookieName = "visitDate="; function fncCheckCookie() { var strCookie = document.cookie; document.write("<center><hr>クッキーを使う<hr><br><br>"); //日付情報の取得 dteToday = new Date(); myYear = dteToday.getFullYear(); myMonth = dteToday.getMonth() + 1; myDate = dteToday.getDate(); strCookieDate = myYear + "年" + myMonth + "月" + myDate + "日"; //初訪問日の取得 if (strCookie.indexOf(strCookieName) < 0) { document.write("初訪問日は" + strCookieDate + "です。<br>"); document.cookie=strCookieName + strCookieDate; //クッキーの読み出し }else{ strPreDate = strCookie.substring(strCookieName.length,strCookie.length); document.write("前回の訪問は" + strPreDate + "でしたね。"); document.cookie= strCookieName + strCookieDate; } //分岐はここまで document.write("</center>"); } window.onload=fncCheckCookie; //--> </script> </head> <body> </body> </html> |
| 【解説】 関数「fncCheckCookie」 変数1「strCookieName」クッキー名 変数2「strCookie」クッキーそのもの 変数3「strCookieDate」初訪問した年月日 機構は前回と同じですが、年月日を設定するのが ややこしくなっています。 |