Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery cookie not detect no exist

When the given cookie does not exist, the result must be null or undefined, but in this case favouritescook doesn't exist but the script thinks it does.

The result must be "OK Detect" for null or undefined, but the result is "BAD no detect result". Why isn't this working?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script>
actcookies=jQuery.cookie("favouritescook");
function fav(id)
{
    if (actcookies=='undefined' || actcookies=='null')
    {
        alert("OK detect");
    }
    else
    {
        alert("BAD Not detect Result : "+actcookies);
    }
}
</script>

<div onclick="fav(1);">Test Now 1</div>

I have tried this in different browsers, with the same result. I don't understand this.

like image 470
Mario Avatar asked Oct 18 '22 16:10

Mario


1 Answers

The problem is it doesn't equal the string 'undefined' or the string 'null', it matches the values undefined and null, without the quotes ('').

Ditch the quotes and it should work just fine.

like image 111
samanime Avatar answered Oct 21 '22 09:10

samanime