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 "";
}

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.
}
commented: His comments are very cooool +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.