0

我是 javascript 新手,我在编写 cookie 时遇到问题。这是我的代码

    function writeCookie(name, value, days){
        var cookie_expires;
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            cookie_expires = "; expires=" + date.toGMTString();
}
else {
    cookie_expires = "";
}
document.cookie = name + "=" + value + cookie_expires;

    }

然后如果我调用这个函数

function check(){
      alert("all cookies"+document.cookie);
 }

它什么也没给我。有人能帮我吗?

4

1 回答 1

0

尝试使用这些函数来编写和获取 cookie:

function setCookie(c_name,value,exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
       x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
       y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
       x=x.replace(/^\s+|\s+$/g,"");
       if (x==c_name)
       {
           return unescape(y);
       }
    }
}
于 2013-01-10T02:02:44.117 回答