JQuery cookie does not detect that no

If this cookie does not exist, the result should be null or undefined , but in this case, the favoritescook does not work, but the script thinks it is.

The result should be "OK Detect" for null or undefined, but the result is "BAD no detect result". Why is this not 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 tried this in different browsers with the same result. I do not understand this.

+5
source share
3 answers

it will not return undefined you need to change your controller as

 if (typeof actcookies=='undefined' || !actcookies) { alert("OK detect"); } else { alert("BAD Not detect Result : "+actcookies); } 
+1
source

The problem is that it is not equal to the string 'undefined' or the string 'null' , it corresponds to the values undefined and null without quotes ( '' ).

Mark the quotation marks and it should work fine.

+2
source

it should work

  actcookies=jQuery.cookie("favouritescook"); function fav(id) { if (actcookies==undefined || actcookies==null) { alert("OK detect"); } else { alert("BAD Not detect Result : "+actcookies); } } 
0
source

Source: https://habr.com/ru/post/1265255/


All Articles