JavaScript Cookie长度

时间:2011-01-06 22:38:02

标签: javascript cookies

我只是在学习JS cookies。我可以在一个人按下按钮时保存cookie,但似乎周期的长度不正确。我需要它15天,但它一直说时间长度直到会议结束(http://screencast.com/t/ea7TOoGTnbA)。

以下是我在下面使用的代码:

<script type="text/javascript">
  function getCookie(w){
    cName = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split('; ');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0] == w){
            cName = unescape(NmeVal[1]);
        }
    }
    return cName;
}
function printCookies(w){
    cStr = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split('; ');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0]){
            cStr += NmeVal[0] + '=' + unescape(NmeVal[1]) + '; ';
        }
    }
    return cStr;
}
function setCookie(name, value, expires, path, domain, secure){
    document.cookie = name + "=" + escape(value) + "; ";

    if(expires){
        expires = setExpiration(expires);
        document.cookie += "expires=" + expires + "; ";
    }
    if(path){
        document.cookie += "path=" + path + "; ";
    }
    if(domain){
        document.cookie += "domain=" + domain + "; ";
    }
    if(secure){
        document.cookie += "secure; ";
    }
}

function setExpiration(cookieLife){
    var today = new Date();
    var expr = new Date(today.getTime() + cookieLife  * 60 * 60 * 10);
    return  expr.toGMTString();
}

</script>

<script language="JavaScript">
// set a cookie which will expire in 3 days and be accessible site wide
setCookie('drunkdriving_cta_overlay', 'Yes', 3, '/');
</script>


<script language="JavaScript"><!--
document.write(getCookie("drunkdriving_cta_overlay"));
//-->
</script> 

<?php if (isset($_COOKIE["drunkdriving_cta_overlay"])) {?>
hello?
<?php } else { ?>
goodby
<?php } ?>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

看起来问题不在于设置cookie,而是在日期计算中。尝试用以下代码替换setExpiration方法:

function setExpiration(cookieLife){ 
    var expires = new Date();
    expires.setDate(expires.getDate()+cookieLife);
    return expires.toGMTString();
}

您方法中的日期计算似乎无法正常工作。

编辑:

请尝试使用此代码。我想我发现了这个问题。

<script type="text/javascript">
  function getCookie(w){
    cName = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split(';');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0] == w){
            cName = unescape(NmeVal[1]);
        }
    }
    return cName;
}
function printCookies(w){
    cStr = "";
    pCOOKIES = new Array();
    pCOOKIES = document.cookie.split(';');
    for(bb = 0; bb < pCOOKIES.length; bb++){
        NmeVal  = new Array();
        NmeVal  = pCOOKIES[bb].split('=');
        if(NmeVal[0]){
            cStr += NmeVal[0] + '=' + unescape(NmeVal[1]) + ';';
        }
    }
    return cStr;
}
function setCookie(name, value, expires, path, domain, secure){
    var cookie = name + "=" + escape(value);

    if(expires){
        expires = setExpiration(expires);
        cookie += ";expires=" + expires;
    }
    if(path){
        cookie += ";path=" + path;
    }
    if(domain){
        cookie += ";domain=" + domain;
    }
    if(secure){
        cookie += ";secure";
    }
    document.cookie = cookie;
}

function setExpiration(cookieLife){
    var expires = new Date();
    expires.setDate(expires.getDate()+cookieLife);
    return expires.toGMTString();
}

</script>

<script language="JavaScript">
// set a cookie which will expire in 3 days and be accessible site wide
setCookie('drunkdriving_cta_overlay', 'Yes', 3, '/');
</script>


<script language="JavaScript"><!--
document.write(getCookie("drunkdriving_cta_overlay"));
//-->
</script>