Check if cookie exists not working

Mina Hafzalla

I'm using the below code to check if a cookie exists with a certain value then do something.

$(document).ready(function() {
   if (document.cookie.indexOf('samplename=itsvalue')== -1 ) {
      alert("cookie");
   }
});

It always displays the alert whether the cookie exists or not. What I'm doing wrong here?

buzzsaw

Original response:

Unless you have a cookie with the key "samplename=itsvalue", your code will always evaluate to true. If the key is "samplename" and the value is "itsvalue", you should rewrite the check like this:

if (document.cookie.indexOf('samplename') == -1 ) {
  alert("cookie");
}

This will tell you that the cookie does not exist.

To see if it does exist:

if (document.cookie.indexOf('samplename') > -1 ) {
  alert("cookie exists");
}

Updating to better address this question:

What you are looking for in that check will always evaluate to true and throw the alert. Add the following function to your js and call it to check if your cookie exists.

function getCookie(name) {
    var cookie = document.cookie;
    var prefix = name + "=";
    var begin = cookie.indexOf("; " + prefix);
    if (begin == -1) {
        begin = cookie.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
        end = cookie.length;
        }
    }
    return unescape(cookie.substring(begin + prefix.length, end));
} 

You can then check your cookies with the following:

var myCookie = getCookie("samplename");

if (myCookie == null) {
    alert("cookie does not exist");
} else {
    alert("cookie exists");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

check cookie if cookie exists

Javascript - Check if cookie exists

Check if Cookie exists with JSP EL

Check if a PHP cookie exists not worked

Check if a cookie array element exists

Check if username exists, not working

Check if WordPress password protect cookie exists

Check if OU exists not working properly

check if file exists bash not working

(Updated) Working with files, check if exists or not

Code to check if file exists in JQuery is not working

git check if working copy repository exists and validate it

NSIS - check if process exists (nsProcess not working)

Firebase Swift check user if exists not working properly

Check if email already exists in database - not working

Chrome extensions api doesn't check if cookie exists

Check to see if a cookie exists based on part of its name in PHP

My python code to check for element exists in a list is not working

What's the sure-fire way to check if a sensor exists and IS WORKING?

Contains not working properly (Check if an element already exists in DataGrid)

Check if an ID exists in another table using NOT IN is not working - MySQL

jQuery if cookie exists in array

Prepending on load if cookie exists

Checking if no Cookie exists and if a Cookie exists, delete the Cookie in Testcafe

"not in" is working but "not exists" is not working in hql

Change a cookie value of a cookie that already exists

How to hide a div if cookie exists

Why when Check if the user already exists in ASP.NET MVC database first not working?

Check for a cookie with Python Flask