IzzyTwinkly
asked on
function to get a cookie
Hi guys,
I have the following function that gets a cookie. But I am having a hard time to understand the following code.
Can anybody explain this line by line?
if I call the function as follow
getCookie("Izzy")
what would happen to the code?
I don't understand from the beginning. why does name store cname + "=", not just cname?
I have the following function that gets a cookie. But I am having a hard time to understand the following code.
Can anybody explain this line by line?
if I call the function as follow
getCookie("Izzy")
what would happen to the code?
I don't understand from the beginning. why does name store cname + "=", not just cname?
function getCookie(cname) {
var name = cname + "=";
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);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Simple explanation!
Cookie is a Key-Value paired collection to stores the site/page related information at the client computer to use within the page or while making the request!
ame2=value 2;cname=cv alue; etc.
and it returns back it's value like "somevalue"
Cookie is a Key-Value paired collection to stores the site/page related information at the client computer to use within the page or while making the request!
I don't understand from the beginning. why does name store cname + "=", not just cname?As it is a collection, if we need a particular value how could we identify that? That's why it is added like Key-Value paired separated by semicolon(;) for e.g. cookieName1=value1;cookieN
what would happen to the code?In your function
getCookie("Izzy")it asks to get the value of key "lzzy"! In cookies it is stored as : lzzy=somevalue;
and it returns back it's value like "somevalue"
ASKER
Thanks guys!
Please correct me if I am wrong.
so if I have a cookie like "name1=value1; name2=value2; cname=cvalue",
ca[0] contains "name1=value1"
ca[1] contains "name2=value2"
ca[2] contains "cname=cvalue"
when we loop through with ca
var c= ca[0]; // in here c will contain "name1=value1"
and it has while loop chekcing c.charAt(0)==' '. However, there is no ' ' in "name1=value1".
so c.substing(1) wouldn't be executed.
"name1=value1".indexOf("cn ame=")=0 is not true, so c.substring(name.length, c.length) wouldn't be executed.
The same process will be applied for ca[1], "name2=value2".
Now, for ca[2], "cname=cvalue",
c = "cname=cvalue"
again while(c.charAt(0)=' ') is false, c=c.substring(1) wouldn't be executed.
However, "cname=cvalue".indexOf("cn ame=") ==0 is true,
"cname=cvalue".substring(6 , 12) will be returned, which is 'cvalue'.
Now I understand it, but I am wondering,
why would we need
while(c.charAt(0)==' ')
c=c.substring(1);
?
Please correct me if I am wrong.
so if I have a cookie like "name1=value1; name2=value2; cname=cvalue",
ca[0] contains "name1=value1"
ca[1] contains "name2=value2"
ca[2] contains "cname=cvalue"
when we loop through with ca
var c= ca[0]; // in here c will contain "name1=value1"
and it has while loop chekcing c.charAt(0)==' '. However, there is no ' ' in "name1=value1".
so c.substing(1) wouldn't be executed.
"name1=value1".indexOf("cn
The same process will be applied for ca[1], "name2=value2".
Now, for ca[2], "cname=cvalue",
c = "cname=cvalue"
again while(c.charAt(0)=' ') is false, c=c.substring(1) wouldn't be executed.
However, "cname=cvalue".indexOf("cn
"cname=cvalue".substring(6
Now I understand it, but I am wondering,
why would we need
while(c.charAt(0)==' ')
c=c.substring(1);
?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Refer lines 20-24 of the first code listing in my first post
The javascript trim() function could have been used as well
// IGNORE ANY WHITESPACE FROM THE START
// OF THE STRING BY ADVANCING A VAR (c)
// THROUGH THE ARRAY AND CHECKING FOR
// BLANK CHARS
while (c.charAt(0)==' ') c = c.substring(1)
The code makes sure there is no white space leading up to the start of the name so an accurate comparison of the key can be made. There should not be any WS there but it is a safety net in case.The javascript trim() function could have been used as well
var c = ca[i].trim();
ASKER
Thanks everybody including Dave.
Now I understand the code!!!
Now I understand the code!!!
You are welcome.
name=value; name2=value2; name3=value3
This simple page will show you all the contents of 'document.cookie' on your site.
Open in new window