2012-02-17 418 views
2

我已经使用document.cookie设置了Cookie。 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*/ 

} 
+0

如果您需要任何帮助,请告诉我们您的代码。 – Leo 2012-02-17 19:39:11

+0

此脚本在哪里运行?内容脚本或背景/弹出? – yonran 2012-02-17 19:55:34

回答

4

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