Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]Reading Cookie returns NULL[/b]

Status
Not open for further replies.

dobrios

Programmer
Dec 27, 2000
54
0
0
US
Hello All,
Sorry to ask very basic question but I'm a little bit rusty at the moment on JavaScript as used it 2 something years ago. I tried some different functions from different posts, but no luck.

I have a project where I need to read a value from URL, set cookie on one page and when user goes to shopping checkout page, retrieve value of my cookie.

I read value from URL and set cookie Ok, as I don't get errors and verify that cookie exist through netscape cookie manager.

But on checkout page, when I try to read cookie, I get Null value. Anything am I doing wrong?
Thanks much.Here's code:

<script language="JavaScript">
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));

}

function showCookie(){
var myCookie = getCookie("Affiliate_ID")
alert(myCookie);
}
</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" onLoad="javascript:showCookie()">

 
>>var end = document.cookie.indexOf(";", begin);

should be:
var end = document.cookie.indexOf(";"+ begin);


Known is handfull, Unknown is worldfull
 
Thanks for the reply "vbkris".
I corrected code but still get Null when read cookie.
 
one more thing name is a reserved keyword...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top