/    Sign up×
Community /Pin to ProfileBookmark

reading a cookie

I have created a function that writes a cookie with certain variables in it:


function writeData(name, allpicsSize, numberofPictures)
{

parent.document.cookie=”name=” + escape(name) + “; allpicsSize=” + escape(allpicsSize) + “; numberofPictures=” + escape(numberofPictures)
location.href=”files/pictures_character_overview.html”
}


as you can see, after the cookie is written, another page is loaded. On that page there’s another function to read the cookie and assign the values to the right variables:


var name=readCookie(“name”)
var allpicsSize=readCookie(“allpicsSize”)
var numberofPictures=readCookie(“numberofPictures”)

function readCookie(required)
{
cookie=unescape(parent.document.cookie)
list=cookie.split(‘;’)
data=””

for (counter=0; counter<list.length; counter++)
{
parts=list[counter].split(‘=’)
if (parts[0].substring(0,1)==’ ‘)
{
parts[0]=parts[0].substring(1,parts[0].length)
}
if (parts[0]==required)
{
data=parts[1]
break
}
}
return data
}


But when the page is loaded I find that only the first variable (name) has been given the right value. The other two have no value whatsoever.

Can anyone tell me what I did wrong?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangAug 12.2003 — You have to write 3 seperate cokkies or a string of values.
<i>
</i>// 3 cookies:
parent.document.cookie="name=" + escape(name); <br/>
parent.document.cookie="allpicsSize=" + escape(allpicsSize);
parent.document.cookie="numberofPictures=" + escape(numberofPictures);
// string comma delimiter, NOT a semicolon
parent.document.cookie="MyCookies="+escape(name)+","+escape(allpicsSize)+","+escape(numberofPictures);

If you use the second approach:
<i>
</i>var name="";
var allpicsSize="";
var numberofPictures="";
//Split cookie
var cookieList=document.cookie.split("=");
var crumb=cookieList[1].split(",")
name=crumb[0];
allpicsSize=crumb[1];
numberofPictures=crumb[2];
//Show cookie
alert("name="+name+", allpicsSize="+allpicsSize+", numberofPictures="+numberofPictures)
Copy linkTweet thisAlerts:
@bart_mauthorAug 12.2003 — Well, this accually DOES seem to work ?! Thanks a lot!

But I don't suppose you can easily see what's wrong with my old code? Because I always try to learn from my mistakes?
Copy linkTweet thisAlerts:
@FangAug 12.2003 — cookie format:

name=[I]value[/I]; [expires=[I]date[/I]; [path=[I]path[/I]; [domain=[I]domain[/I] [[I]secure[/I];]]]]

Your first cookie was set, but the other two were seen as an expiry date and the path.
Copy linkTweet thisAlerts:
@bart_mauthorAug 12.2003 — Oke, thanks.
×

Success!

Help @bart_m spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...