use*_*331 3 javascript cookies
I have trying to see if a cookie exists or not, Here is my getCookie method:
function getCookie(name)
{
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
Run Code Online (Sandbox Code Playgroud)
and here is how I calling that method:
var cookie = getCookie("counter");
Run Code Online (Sandbox Code Playgroud)
and here is my condition I tried:
if(cookie == '')
{
}
else
{
//Always goes here even when I clear my cookies and I know it does not exist.
}
Run Code Online (Sandbox Code Playgroud)
My condition always goes into the else, not matter what, what am I doing wrong?
小智 5
可以检查cookie的值是否未定义
if (typeof(cookie) === 'undefined'){
CONSOLE.LOG('no cookie');
} else {
CONSOLE.LOG(' cookie exist');
}
Run Code Online (Sandbox Code Playgroud)