Discussion Locked

Cookies and JavaScript

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Syntax for a "cookie dictionary" or "name-value pairs"? Here's an example of what I need. I have 3 form buttons labeled Bob, Bill, and Betty. I have a cookie named myCookie. With ASP, I can click a button and write:
Response.Cookies("myCookie")("User")=("Bob")
or change it to
Response.Cookies("myCookie")("User")=("Bill")
Still just 1 cookie. How do I do this with JavaScript?

7 replies

by mr. origami

Wow. That was more than I expected - but not really what I needed. My fault for not being exactly clear. This is for a JavaScripted shopping cart application. When a user clicks an item's checkbox, JS determines if the box is checked or not, then adds/adjusts the cookie. The checkbox might be named "bill", so onClick=setCookie(this.name,this.checked) leads to setCookie(property,value) and document.cookie=property+"="+value.

Problem is this creates a NEW cookie each time. Doesn't matter if a user unchecks a box, the cookie is still there (just set to "false"). So - after 20 clicks (20 cookies) the cart fails as browsers can only handle 20 cookies. The solution - use 1 cookie with name-value keys.

I need 3 variables to reference my cookie like I can do with ASP. I can easily create Response.Cookies("myCookie")("bill")=("true") then add Response.Cookies("myCookie")("sam")=("true"). One cookie with 2 pieces of information. With JS I try document.cookie="myCookie:bill=true" and "myCookie:sam=true" - but this makes 2 cookies (myCookie:bill and myCookie:sam) rather than one named "myCookie". If I try "myCookie=bill:true" and "myCookie=sam:true", the bill is lost as sam overwrites it.

by andrew

The example I gave only creates one cookie, overwriting the previous value each time. If you need to store multiple values in then same cookie, use different names:

document.cookie = 'sam=true';
document.cookie = 'bill=false';
document.cookie = 'bob=true';

This will create one cookie with 3 different values in it.

If you want to change a value, no problem:

document.cookie = 'bob=false';

Still 1 cookie.

by mr. origami

I have done this:
document.cookie = 'sam=true';
document.cookie = 'bill=false';
document.cookie = 'bob=true';
but it fails after 20 clicks - like the browser has used its "20 cookie allotment". Why is this if I actually have only 1 cookiewith 20+ values?

by andrew

That's a good question. My example works long after 20 clicks, and I have verified (by looking at the cookie files themselves) that only one cookie is created.

What browser are you using? I'm on IE 5.5.

by mr. origami

Maybe the problem is with the other cookie. I have a logon cookie created solely by ASP to create a "username".
Response.Cookies("info")("user")=("bob")
or whatever name they type in the form.
Then, the shopping cart items are stored with javascript cookies. Users can't get to the shopping area until they provide a username. But after 20 items or so are clicked, and I click ViewCart or something, I get booted out to the logon page and it doesn't remember who I am - like the cookie created with ASP is getting deleted. The ASP cookie has an expiration date of Jan 1,2002 (in the correct format) while the JS cookies have no expiration date (they should be good only for the session).

(Not that it matters - but this isn't a true shopping cart. It's for an intranet in a printing company where my sales department can select which elements (backgrounds, fonts, frames, etc...) they want our art department to use on the product (post card, brochure, etc...).

I appreciate your time so far. Hope we can get this worked out...

This discussion is closed to new replies.