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?
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?
Share Improve this question edited Dec 6, 2012 at 17:35 Martin Borthiry asked Dec 6, 2012 at 15:37 Martin BorthiryMartin Borthiry 5,32610 gold badges43 silver badges59 bronze badges 4- "Has a value" as in "has any value" or "has exactly this value"? – Ry- ♦ Commented Dec 6, 2012 at 15:38
-
2
I'm going to name my cookie,
MYCOOKIENAME
, and give it a value ofVALUES
. What do you think is going to happen? – zzzzBov Commented Dec 6, 2012 at 15:45 -
Well, indexOf would only evaluate correct if you check for the containment of a String in a cookie, it doesn't match a plete name, in that case the above would return
false
therefore giving you the wrong result. – Moritz Roessler Commented Dec 6, 2012 at 16:03 - Shouldn't that be document.cookie.indexOf('COOKIENAME=') != -1 if we want to check if a cookie exists with that name.. – Esko Commented Dec 8, 2017 at 7:13
3 Answers
Reset to default 4I would suggest writing a little helper function to avoid what zzzzBov mentioned in the ment
- 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 plete 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
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");
I use Jquery cookie plugin for this.
<script type="text/javascript" src="jquery.cookie.js"></script>
function isCookieExists(cookiename) {
return (typeof $.cookie(cookiename) !== "undefined");
}