Ok, lets say you have 3 cookies source, id, and sourceid. In this case every GetCookie I have seen will fail if the sourceid cookie comes first. Here is a GetCookie that will not. And for those that say no one will ever do this, I've tripped across this on web sites at three different places.
function GetCookie(name) {
var cookies = document.cookie;
var startIndex = 0;
while(cookies.length > 0) {
startIndex = cookies.indexOf(name + "=");
if (startIndex == -1)
return null;
if (startIndex == 0)
break;
if ((cookies.charAt(startIndex-1) == ' ') || (cookies.charAt(startIndex-1) == ';'))
break;
cookies = cookies.substring(startIndex + name.length + 1);
}
if (cookies.length == 0)
return null;
var endIndex = cookies.indexOf(";", startIndex);
if (endIndex == -1)
endIndex = cookies.length;
return unescape(cookies.substring(startIndex+name.length+1, endIndex));
}


Comments