2012-02-11 87 views
0

我寫了下面的函數來設置一個cookie.Here $。$域名保存域名。Javascript cookies。路徑沒有設置

function $cookie(name,value,lifeTime,path,domain,secure){//lifetime in hours 
     if(typeof name == 'undefined' || name == ''){ 

      alert('ERROR !!!\n Name of the cookie is not passed \n'); 
      return false; 
     } 

     if(typeof value == 'undefined'){ 

      alert('ERROR !!!\n Value of the cookie '+name+' is not passed \n'); 
      return false; 
     } 
     var c_ = name +'='+escape(value)+';'; 

      if(typeof lifeTime != 'undefined'&&lifeTime != ''){ 
       var life = new Date(); 
       lifeTime<1&&lifeTime>0life.setMinutes(life.getMinutes()+lifeTime*60):life.setHours(life.getHours()+lifeTime); 
       life = life.toUTCString(); 
       c_+='expires='+life+";"; 
     } 
     else lifeTime = 'Unspecified'; 

     if(typeof domain != 'undefined' && domain != '') 
       c_+= 'domain='+domain+';'; 
     else 
      domain = $.$domain; 
     if(secure == 1) 
      c_ += 'secure=secure;';//secure 
     else 
         secure = 0; 
     if(typeof path != 'undefined' && path != '') 
      c_ += 'path='+path+';'; 
     else 
         path = '/'; 
     document.cookie = c_; 
     this.$name = name; 
     this.$value = value; 
     this.$life = lifeTime; 
     this.$domain = domain; 
     this.$secure= secure; 
     this.$path = path; 
     if(!$.$COOKIE) 
      $.$COOKIE = {}; 
     if(lifeTime<0) 
     { 
      var t; 
      if(t = $.$COOKIE[name]) 
      { 
       delete t.$name; 
       delete t.$value; 
       delete t.$life; 
       delete t.$domain; 
       delete t.$secure; 
       delete t.$path; 
       delete $.$COOKIE[name]; 
      } 
     } 
     else 
     $.$COOKIE[name] = this; 

     return this; 
    } 
      var a = new $cookie('a2',6,1,'./scripts',''); 

$。$ COOKIE持有由域設置的所有Cookie .. 腳本是在我的根目錄的目錄.. 後創建的,我認爲谷歌瀏覽器cookie的,但道路是不set.It設置爲我的/目錄。爲什麼會出現這種情況

回答

0

似乎

if(typeof domain != 'undefined' && domain != '') 
     c_+= 'domain='+domain+';'; 
     else 
     domain = $.$domain; 

應該

if(typeof domain != 'undefined' && domain != '') 
     c_+= 'domain='+domain+';'; 
     else 
     c_+= 'domain='+$.$domain+';'; 
+0

:如果沒有在document.cookie域中指定域,那麼將設​​置cookie的頁面域?我是對的嗎? 這就是爲什麼我沒有將它添加到document.cookie.It是隻有當用戶指定域時添加 – 2012-02-11 18:56:22

+0

我明白$。$域是全球性的,並且你想要爲域使用該值。 – 2012-02-11 19:01:33

+0

好的..爲什麼路徑沒有被設置? – 2012-02-11 19:16:32