2016-08-30 6 views
-1

getCookie(x)이라는 함수가 있는데이 쿠키는 이름이 x 인 쿠키의 값을 반환합니다. console.log(getCookie(foo));으로 확인하면 올바른 용어가 solved이거나 쿠키가 존재하지 않으면 빈 문자열이 반환됩니다. Foo은 변수입니다! 이 테스트에 대한compare 쿠키의 값이 매번 "false"를 반환합니다.

, 내 쿠키 foo이 존재하고 값이 "solved" 그러나 나는이 작업을 수행 할 경우 :

console.log(getCookie(foo) == "solved"); 

는 false를 반환합니다. 왜 이런거야?

이 내 getCookie() 기능입니다 : 그들은 영어로하지 않기 때문에 내가 코멘트를 삭제

function getCookie(cname){                  
       var name = cname + "=";               
       var carray = document.cookie.split(";");           
       for(var j=0; j <carray.length; j++){            
         var cookie = carray[j];             
         while(cookie.charAt(0)==" "){           
          cookie = cookie.substring(1);          
         } 
         if (cookie.indexOf(name) == 0){           
          return cookie.substring(name.length,cookie.length); //retruns the value of the cookie, in my case it is always "solved" 
         } 
       } 
       return ""; //if there is no cookie with the given name, it returns an empty string                 
} 

. 내 쿠키에도 만료일이 있기 때문에이 부분을 cookiename=value 부분으로 가져 가야합니다.

+0

은'foo' 변수이거나''foo "'를 쓰고 있다는 뜻입니까? –

+1

나는 getoverflowflow.com에서 코드를 테스트했다 :'getCookie ('_ ga')'가 작동한다. 그래서 나는 문제를 재현 할 수 없다. –

+0

typeof로 쿠키 변수를 검사하고 "object"를 반환했습니다. 내가 getCookie 함수에 객체를 주었을 때 이것이 false로 반환 된 이유! –

답변

0

나는 console.log(typeof(foo)); 을 확인했으며 어떻게 든 그것이 내 문자열 comparion이 "false"를 반환 한 이유입니다.