General discussion

Locked

Cookies and JavaScript

By Mr. Origami ·
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?

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Cookies and JavaScript

by andrew In reply to Cookies and JavaScript

here's an example:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>

<input type=button value='Bill' id=btn1 onClick='JavaScript:SetCookie();'><br>
<input type=button value='Bob' id=btn2 onClick='JavaScript:SetCookie();'><br>
<input type=button value='Betty' id=btn3 onClick='JavaScript:SetCookie();'><br>
<p>
<input type=button value='Get Cookie' id=btn4 onClick='JavaScript:GetCookie();'><br>

</BODY>
<script language=JavaScript>
function SetCookie()
{
//cookie will expire in a year
var nextyear = new Date();
nextyear.setFullYear(nextyear.getFullYear() + 1);
//set the cookie
document.cookie = 'user=' + event.srcElement.value + ';expires=' + nextyear.toGMTString();
}

function GetCookie()
{
var cookies = document.cookie;
var pos = cookies.indexOf('user=');

if(pos != -1) //if the cookie is there, parse out the value...
{
var start = pos + 5;
var end = cookies.indexOf(';');
if(end == -1) end = cookies.length;
var value = cookies.substring(start,end);
alert('User=' + value);
}

}

</script>
</HTML>

Collapse -

Cookies and JavaScript

by Mr. Origami In reply to Cookies and JavaScript

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.

Collapse -

Cookies and JavaScript

by andrew In reply to Cookies and JavaScript

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.

Collapse -

Cookies and JavaScript

by Mr. Origami In reply to Cookies and JavaScript

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?

Collapse -

Cookies and JavaScript

by andrew In reply to Cookies and JavaScript

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.

Collapse -

Cookies and JavaScript

by Mr. Origami In reply to Cookies and JavaScript

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...

Collapse -

Cookies and JavaScript

by Mr. Origami In reply to Cookies and JavaScript

This question was closed by the author

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums