I’m cleaning up my folder Webstuff, and came across another read cookie javascript than I normally use. I wonder which one would actually be best, if at all, and why, or what the practical differences are:
function readCookie(name) {
if (name == "") return "";
var strCookie =" " + document.cookie;
var idx = strCookie.indexOf(" " + name + "=");
if (idx == -1) idx = strCookie.indexOf(";" + name + "=");
if (idx == -1) return "";
var idx1 = strCookie.indexOf(";", idx+1);
if (idx1 == -1) idx1 = strCookie.length;
return unescape(strCookie.substring(idx + name.length+2, idx1));
}
or
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}