正在设置 Cookie。但是 document.cookie 为空

Jin*_*iel 3 javascript cookies google-chrome

我已经设置了一个 cookie document.cookie。谷歌浏览器中的内容设置显示了 cookie。但是,document.cookie打印时显示为空白字符串。为什么会这样?

这是我的代码

function setCookie(name,value,lifeTime,path,domain,secure){//lifetime in hours
{
         var c_ = name +'='+escape(value)+'; ';

            var life = new Date();
            lifeTime<1&&lifeTime>0?life.setMinutes(life.getMinutes()+lifeTime*60):life.setHours(life.getHours()+lifeTime);
            life = life.toUTCString();
            c_+='expires='+life+"; ";




        c_+= 'domain='+domain+'; ';


        c_ += 'secure=secure; ';//secure 

        c_ += 'path='+path;

        document.cookie = c_;
        alert(document.cookie);
/*Just splitted the code instead of c = 'name='+value+'expires ='+life  etc*/

}
Run Code Online (Sandbox Code Playgroud)

Raf*_*ael 6

这个函数的一个可能的问题是,它总是设置一个安全的 cookie。因此,如果您使用 HTTP 而非HTTPS协议请求/打开页面,则不会公开安全 cookie。