Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

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);
}
about 4 years ago · Santiago Trujillo Report

0

this should work

    actcookies=jQuery.cookie("favouritescook");
function fav(id)
{
    if (actcookies==undefined || actcookies==null)
    {
        alert("OK detect");
    }
    else
    {
        alert("BAD Not detect Result : "+actcookies);
    }
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!