0

I would like to know the working of this code in brief.. any help will be appreciated...

function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Airshow
1

Umesh,

Cookies are small text files containing a set of concatenated strings each comprising a name=value pair, and delimited with a ";" (semicolon).

A cookie is simply retreived by javascript as document.cookie.

If no cookie has been set, then document.cookie is "" (empty string).

function getCookie(c_name)//function which will return the (string) value for the c_name substring of the entire cookie.
{
	if (document.cookie.length>0)//if cookie has been set for this document
	{
		c_start = document.cookie.indexOf(c_name + "=");//find start position of required substring
		if (c_start!=-1)//if required substring exists
		{
			c_start = c_start + c_name.length + 1;//shift start position right by the length of the name of the required substring
			c_end = document.cookie.indexOf(";", c_start);//find position of next delimiter ";"
			if (c_end == -1) c_end = document.cookie.length;//if delimiter not found then set end position to end of whole cookie string
			return unescape(document.cookie.substring(c_start,c_end));//extract the required substring, and return it, unescaped.
		}
	}
	return "";//if anything above fails, then return an empty string.
}
Votes + Comments
His comments are very cooool
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.