日期:2014-05-16 浏览次数:20382 次
function getCookie(cName) { var sCookie = document.cookie; if (sCookie.length < 1) return false; else { var ar = sCookie.split(';'); for (var i = 0; i < ar.length; i ++) { var ar_tmp = ar[i].split('='); if (ar_tmp[0] == cName) return ar_tmp[1]; } return false; } } if (!getCookie('firstViewTime')) { var d = new Date(), ar_date = [], ar_time = []; ar_date.push(d.getFullYear()); ar_date.push(d.getMonth() + 1); ar_date.push(d.getDate()); ar_time.push(d.getHours()); ar_time.push(d.getMinutes()); ar_time.push(d.getSeconds()); document.cookie += 'firstViewTime=' + ar_date.join('-') + ' ' + ar_time.join(':'); } else alert('首次访问时间:' + getCookie('firstViewTime'));
------解决方案--------------------
<script type="text/javascript">
function init(){
var d=new Date();
var cookie=document.cookie;
if(cookie.indexOf("firsttime=")==-1){
document.cookie="firsttime="+d+";max-age=100000000";
}else{
var begin=cookie.indexOf("firsttime=");
var end=cookie.indexOf(";",begin);
if(end==-1){
end=cookie.length;
}
cookie=cookie.substring(begin+"firsttime=".length,end);
alert(cookie);
}
}
window.onload=init;
</script>
这样试试