Click to See Complete Forum and Search --> : Changing Data In Cookies
chestertb
10-09-2003, 08:57 AM
I think I am reaching the conclusion that javascript sucks big time, and the people/person responsible for creating it will be the first up against the wall come the revolution.
I am creating and changing cookies in my site to remember user settings as they move from page to page.
I can create the cookie... no problem.
I can read the cookie... no problem.
But...
when I write it again to update the data contained in it, it repeats the cookie name at the head of the string.
Here's the code that writes the cookie...
function bakecookie()
{
var anzac = "hyh="+escape(spre+"º"+dnme+"º"+fnme+"º"+lnme+"º"+cnme+"º"+add1+"º"+add2+"º"+town+"º"+stat+"º"+post+"º"+ctry+"º"+wtel+"º"+mtel+"º"+eml1+"º"+spln+"º"+curr+"º"+rdte+"º"+ipad);
document.cookie = anzac;
}
Here's the code that reads the cookie...
function eatcookie()
{
var cookietext = document.cookie;
var mintslice = unescape(cookietext);
kettle = mintslice.split("º");
alert(mintslice);
}
Frankly, I couldn't be bothered coding all that "indexOf" stuff that's in library cookie scripts, so I just take everything, split it into the array "kettle" (which is why the +"º"+ is in the baking recipe) and manipulate the contents from there.
You'll notice that I have put an alert at the end of the eatcookie function.
The first time it runs through the bake/eat sequence, "mintslice" looks something like
hyh=ºdomain.comºchestertºbearº... etc
the second time, it looks something like
hyh=hyh=ºdomain.comºchestertºbearº... etc
and the third time
hyh=hyh=hyh=ºdomain.comºchestertºbearº... etc.
Because I discard the first element of the array anyway, it doesn't matter all that much, but it's messy and there's a part of my overtaxed brain that would just like to know what's gone wrong.
Anyone help me out here?
Ian
ps... in case you're wondering... anzac and mintslice are both types of cookies... the edible type
Vladdy
10-09-2003, 09:28 AM
Originally posted by chestertb
I think I am reaching the conclusion that javascript sucks big time, and the people/person responsible for creating it will be the first up against the wall come the revolution.
<snip>
... and when you smash your finger driving a nail in, it is certainly the fault of a hammer and those who designed it....
:rolleyes:
chestertb
10-09-2003, 09:52 AM
Of course it's the fault of the hammer designer... or maybe it's the nail designer... no... wait... it's the wood designer 'cause if the wood was designed properly in the first place, we wouldn't need the hammer or the nails!
Vladdy
10-09-2003, 09:55 AM
Originally posted by chestertb
Of course it's the fault of the hammer designer... or maybe it's the nail designer... no... wait... it's the wood designer 'cause if the wood was designed properly in the first place, we wouldn't need the hammer or the nails!
And how much do you expect to accomplish with that attitude :D :rolleyes: :D
chestertb
10-09-2003, 10:05 AM
Ok... point to you.
Just trying to preserve my sanity here by injecting a little humour... but of course, at 1am, it's easy to forget that humour is often lost cross border. Ah well.
In any event, I really have spent a considerable chunk of time trying to find the answer to my original question... remember... the one about adding the name of the cookie to the head of the string each time it writes.
Are you able (or perhaps willing) to assist?
Vladdy
10-09-2003, 10:48 AM
I'm certainly able to help, but my willingness is negatively affected by your "introduction" to the problem.
Before attemting to manipulate cookies (or any kind of data for that matter) you need to understand the structure of that data: http://www.ietf.org/rfc/rfc2965.txt
If you do not want to bother learning the manipulation of the data but only interested in using it, get one of the many cookie scripts available.
chestertb
10-09-2003, 12:04 PM
Thank you for referring me to the Lucent Technologies paper...
"3.3.3 Cookie Management If a user agent receives a Set-Cookie2 response header whose NAME is the same as that of a cookie it has previously stored, the new cookie supersedes the old when: the old and new Domain attribute values compare equal, using a case- insensitive string-compare; and, the old and new Path attribute values string-compare equal (case-sensitive)."
QED!
I find it difficult to understand how a line of code that says
x = "a"+"b"+"c" becomes abc the first time it runs, aabc the second time, aaabc the third time etc.
Vladdy, it is clear that my sense of humour has offeneded you and it is perhaps best that I look elsewhere for the answer.
IB
Vladdy
10-09-2003, 12:24 PM
Each cookie begins with a NAME=VALUE pair, followed by zero or more
semi-colon-separated attribute-value pairs. The syntax for
attribute-value pairs was shown earlier. The specific attributes and
the semantics of their values follows. The NAME=VALUE attribute-
value pair MUST come first in each cookie. The others, if present,
can occur in any order. If an attribute appears more than once in a
cookie, the client SHALL use only the value associated with the first
appearance of the attribute; a client MUST ignore values after the
first.
The NAME of a cookie MAY be the same as one of the attributes in this
specification. However, because the cookie's NAME must come first in
a Set-Cookie2 response header, the NAME and its VALUE cannot be
confused with an attribute-value pair.
NAME=VALUE
REQUIRED. The name of the state information ("cookie") is NAME,
and its value is VALUE. NAMEs that begin with $ are reserved and
MUST NOT be used by applications.
The VALUE is opaque to the user agent and may be anything the
origin server chooses to send, possibly in a server-selected
printable ASCII encoding. "Opaque" implies that the content is of
interest and relevance only to the origin server. The content
may, in fact, be readable by anyone that examines the Set-Cookie2
header.
You code line is not x="a"+"b"+"c"
You are setting cookie by creating a sting:
name = value 1 [separator] value 2 [separator] value 3 ...
When splitting the value using the separator the name of the cookie ends up being part of the value 1.
chestertb
10-09-2003, 12:55 PM
Thanks Vladdy,
I understand that part. That's why, when I split the string in the cookie, I ignore/discard the first element of the array. The name becomes part of that first element when I split the string out into the individual elements.
I know my eatcookie() function for actually reading the cookie is unorthadox but it actually works rather well and cuts down the code to just three short lines. The reality is "hyh=hyh=hyh=hyh" etc could appear quite a few more times at the beginning of the string without causing much concern, but it is messy, it is wrong, and it is, at least to me, a mystery.
What I don't understand is why, when I update the data in the cookie, the name of the cookie repeats.
Please allow me to step through the process of creating/updating the cookie as I understand it, because clearly there's some part I do not understand.
In my bakecookie() function, I assemble the cookie as follows...
anzac = "hyh="+escape(spre+"º"+dnme+"º" etc
As I understand it, "hyh" is the NAME of the cookie. The string I assemble with the "º" separators is just one VALUE. so I have created NAME=VALUE.
My pages call bakecookie() on unLoad so that any value changed by my user on that page is stored in the cookie.
When the next page opens, eatcookie() extracts the data.
As I read the text, my function bakecookie() is always giving the cookie the same name "hyh" and so when is performs
document.cookie=anzac, it is performing
document.cookie="NAME=VALUE" and should therefore replae the old cookie with the new one. But it isn't... it appears to be performing
document.cookie="NAME="+"NAME=VALUE".
One day I might figure it out!
For now, it being 4am, I need to sleep. Thanks for your help.
IB
Vladdy
10-09-2003, 01:28 PM
Must be something else in your code.
The following test page works as expected (checked in Moz)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb">
<head>
<title>Cookie Test/title>
<script type="text/javascript">
function setCookie()
{ cookie = 'time=' + (new Date()).toString();
alert('Set cookie: ' + cookie);
document.cookie = cookie;
}
function getCookie()
{ alert('Get cookie: ' + document.cookie);
}
</script>
</head>
<body onload="getCookie()" onunload="setCookie()">
</body>
</html>
chestertb
10-10-2003, 01:55 AM
Figured it out!
When I constructed the string value, I assembled a dozen or so fields separated by the character "º" (ascii 186, chosen because it is unlikely to be used in any typing). In doing that, it allows me to split the cookie out into an array when I want to read it.
The first element of that array was always going to be discarded as it would contain the cookie name, so I didn't ever bother putting a value into the variable that was used in position 1 (or 0, depending on how you look at it) to begin with.
The string created to save in the cookie was going to be
"hyh="+escape(val1+'º'+val2+'º'+val3+'º'... etc, except that val1 was null so document.cookie would then look something like hyh=ºdom.comºChesterºBear... etc
My logic told me that not putting a value into val1 shouldn't have made a difference as
escape(val1+'º'+val2+'º'+val3+'º'...) is just a single string, but it did. When I put a value into val1 (in this case, val1="x"), the problem went away.
Odd, but fixed, so case closed.
Thanks Vladdy for persisting with it, and for spending the time trying to help.