日期:2014-05-16  浏览次数:20358 次

通过javascript创建一个cookie,记录第一访问网页的时间。
标题的功能怎么实现,求指教,谢谢!

------解决方案--------------------
存到cookie里的时间,你打算怎么用呢?是取出来存到数据库吗?
------解决方案--------------------
JScript code
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>
这样试试