-1

I got a function named getCookie(x) which returns the value of the cookie with name x. When i check with console.log(getCookie(foo)); it returns the correct term of either solved or an empty string if the cookies doesnt exist. Foo is a variable!

For this test, my cookie foo exists and the value is "solved" but if I do this:

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

it returns false. Why is this?

this is my getCookie() Function:

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                                                                 
}

I deleted comments because they are not in english. My cookies also got an expire date, this is why I have to do these "wild" things to get the cookiename=value part.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

i checked console.log(typeof(foo)); and somehow it was an Object, thats why my string comparion returned "false".