Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster and shorter way to check if a cookie exists

What is the shorter and faster way to know if a cookie has a value or exists?

I'm using this to know if exists:

 document.cookie.indexOf('COOKIENAME=')== -1

This to know if has a value

 document.cookie.indexOf('COOKIENAME=VALUE')== -1

Any better? Any problems on this method?

like image 460
Martin Borthiry Avatar asked Dec 06 '12 15:12

Martin Borthiry


People also ask

How do you check if a cookie exists or not?

document. cookie. indexOf('cookie_name='); It will return -1 if that cookie does not exist.

How check cookie is set or not in jQuery?

If this is the case, then you can actually check if a user has enabled cookies or not using the following straightforward jQuery snippet: $(document). ready(function() { var dt = new Date(); dt. setSeconds(dt.

How do you check if a cookie is expired?

Right-click anywhere on a web page, on the website where you want to check the cookie expiration date. Then, open up the developer tools by selecting Inspect or Inspect element, depending on the browser.


2 Answers

I would suggest writing a little helper function to avoid what zzzzBov mentioned in the comment

  • The way you use indexOf, it would only evaluate correct if you check for the containment of a String in a cookie, it doesn't match a complete name, in that case the above would return false therefore giving you the wrong result.

function getCookie (name,value) {
    if(document.cookie.indexOf(name) == 0) //Match without a ';' if its the firs
        return -1<document.cookie.indexOf(value?name+"="+value+";":name+"=")
    else if(value && document.cookie.indexOf("; "+name+"="+value) + name.length + value.length + 3== document.cookie.length) //match without an ending ';' if its the last
        return true
    else { //match cookies in the middle with 2 ';' if you want to check for a value
        return -1<document.cookie.indexOf("; "+(value?name+"="+value + ";":name+"="))
    }
}
getCookie("utmz") //false
getCookie("__utmz" ) //true

However, this seems to be a bit slow, so giving it an other approach with splitting them Those are two other possibilities

function getCookie2 (name,value) {
    var found = false;
    document.cookie.split(";").forEach(function(e) {
        var cookie = e.split("=");
        if(name == cookie[0].trim() && (!value || value == cookie[1].trim())) {
            found = true;
        }
    })
    return found;
}

This one, using the native forEach loop and splitting the cookie array

function getCookie3 (name,value) {
    var found = false;
    var cookies = document.cookie.split(";");
    for (var i = 0,ilen = cookies.length;i<ilen;i++) {
         var cookie = cookies[i].split("=");
         if(name == cookie[0].trim() && (!value || value == cookie[1].trim())) {
              return found=true;
         }
     }
     return found;
};

And this, using an old for loop, which has the advantage of being able to early return the for loop if a cookie is found

Taking a look on JSPerf the last 2 aren't even that slow and only return true if theres really a cookie with the name or value, respectively

I hope you understand what i mean

like image 75
Moritz Roessler Avatar answered Oct 18 '22 04:10

Moritz Roessler


Apparently:

document.cookie.indexOf("COOKIENAME=VALUE");

For me, is faster, but only slightly.

As the test shows, surprisingly, it's even faster to split the cookie up into arrays first:

document.cookie.split(";").indexOf("COOKIENAME=VALUE");
like image 25
Cerbrus Avatar answered Oct 18 '22 05:10

Cerbrus