View Full Version : problem in returning value of a cookie. it returns undefined

10-20-2003, 11:11 PM
Specifically, i belive my problem is in get_saved_cart() function. returns undefined
Or it could be with funciton GetCookie() which is used in get_saved_cart(). I got all the cookie functions from a the internet. Either way, any help would be appreciated.

Code below:

<script language="JavaScript">
## cookie base functions ##
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1){
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));

function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return getCookieVal (j);

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0){
return null;

function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");

function DeleteCookie(name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);

document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

//my code.
function calculate_item(){

var quant = document.item_form.quant.value;
var cost = document.item_form.item_cost.value;
var total = quant*cost;
document.item_form.total.value = total;
return true;

function save_cart_to_cookie() {
var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));

var item_info = new Array();
item_info[0] = document.item_form.item_id.value;
item_info[1] = document.item_form.quant.value;
item_info[2] = document.item_form.item_cost.value;
item_info[3] = document.item_form.total.value;

if(item_info[1]*item_info[2] != item_info[3]){
alert('Totals dont add up');
if (confirm('Save to cart?')) {
SetCookie('cart', item_info, expdate);

function get_saved_cart() {
if(GetCookie('cart') != null){
//alert('Cart total ' + GetCookie('cart') + '.');
var text;
var cookinfo = getCookieVal('cart');
for(var key in cookinfo){
text += key + "=" + escape( cookinfo[key] ) + "&";

<form name="item_form" id="item_form" method="post" action="">
<table width="75%" border="0" cellspacing="0" cellpadding="0">
<td><input name="quant" type="text" id="quant" value="1" /></td>
<td><input name="total" type="text" id="total" /></td>
<td><input name="item_cost" type="hidden" id="item_cost" value="9.99" />
<input name="item_id" type="hidden" id="item_id" value="1" />
<input name="item_total" type="hidden" id="item_total" /></td>
<td><input name="recalculate" type="button" id="recalculate" value="Recalculate Total" onclick="calculate_item();" />
<input name="add_to_cart" type="button" id="add_to_cart" value="Add" onclick="save_cart_to_cookie();" />
<input name="get_cart" type="button" id="get_cart" value="Get Cookie Value" onclick="get_saved_cart();" />

Willy Duitt
10-21-2003, 04:12 AM
It's setting the cookie and displays values
for the cart total and item info but the
text is undefined in the:
function get_saved_cart()

Where is the value of text defined?

function get_saved_cart() {
if(GetCookie('cart') != null){
alert('Cart total ' + GetCookie('cart') + '.');
var text;
var cookinfo = getCookieVal('cart');
for(var key in cookinfo){
text += key + "=" + escape( cookinfo[key] ) + "&";


Willy Duitt
10-21-2003, 04:21 AM
FWIW: I just ran the script using var text=1;
and the script ran correctly. You need to redefine a value
for text before it is called.


10-21-2003, 06:18 AM
you are saying that when you click on the "Get Cookie Value" button you get an alert?
Well i don't get that at all, but i used to.

the var 'text' is getting data from the for loop
text += key + "=" + escape( cookinfo[key] ) + "&";

actually the output of text should be something like
1,1,9.99,9.99 (or similar).

i don't have a clue of why it isn't working... i was playing around with a script that creates cookie arrays (actually just simulates it). I began playing with the code after i got my initial (this) code working. but it didn't work like i expect and i removed it (using undo).

my goal is to save and retrieve the item info in an array that's in the cookie.

maybe there is another javascript solution to save the item info so that i can view it later.

thanks for taking time to look at the code.

Willy Duitt
10-21-2003, 07:06 AM
I took the comments out from in front of the alerts which aloud
them to fire. But to verify that a cookie was set I used the
cookie manager I have on my toolbar to check the

the var 'text' is getting data from the for loop
text += key + "=" + escape( cookinfo[key] ) + "&";

No its not. text is undefined, and that is the problem.

Place this on your page. It will verify if the cookie is set.

<a href="javascript:function dblsp(a){b=/; /g;c=a.replace(b, '\n\n');return c;}if(document.cookie.length<1){alert('no cookie for this site.')}else{alert(dblsp(document.cookie))}">Read Cookie</a>


BTW: My Cookie Value is: 1%2C1234%2C9.99%2C12327.66

Var text=1 (which I set in my previous post) = 1
Non-Breaking Space = %2C
How Many = 1234
Your Hidden Field = 9.99
Total = 12327.66

If you can read the cookie.
What is the value of the other input I submitted?

10-21-2003, 07:31 PM
using you code snippet you posted, i did get an output for the cookie;
it was as follows



I deleted the actual cookie a few times, but i still got the above output.
By deleting i went to c:\windows\cookies found the file and deleted it.

i've also noticed that the cookie seems to be in 'memory' even after i've physically deleted the cookie. interesting.

i did create another cookie using cart1, but i removed all references to it, i thought somehow the word 'cart' was a problem.

back to the problem....
as i said, i did an outpt using your code.

i'm not sure by what you mean:
"What is the value of the other input I submitted?"

as you said, i did give var text a definition. but clicking on the button still didn't do anything.
here's that code:

if(GetCookie('cart') != null){
var text=1; //changed it here, as you said.
//alert('Cart total ' + GetCookie('cart') + '.');
var cookinfo = getCookieVal('cart');
for(var key in cookinfo){
text += key + "=" + escape( cookinfo[key] ) + "&";

it doesn't seem that i've mentioned it, but i'm fairly new to js. i can get around because of prior programming experience, but js is still a little different.

Willy Duitt
10-21-2003, 08:54 PM
I don't have time to look too closely right now.
But check your cookie expires.


Willy Duitt
10-22-2003, 12:33 PM
The script is working with the exception of :

for(var key in cookinfo){
text += key + "=" + escape( cookinfo[key] ) + "&";

Both text and key are undefined.
I only added a value of 1 to var text; to test
if the value would get passed to the cookie. Which it does.

Is there more to this script? Where have you defined the
values of text and key prior to trying to pass them to the cookie?

What exactly are you trying to read in the cookie other
than the amount ordered, previous balance and total?

Did you check your cookie expires?
I would assume this would be a session cookie.
You would not want a customer who ordered something
two monthes ago return and have thier previous order total
added to a completely seperate order. (I'm still carrying the cookie)


Willy Duitt
10-22-2003, 01:48 PM
FWIW: I checked the cookie expires and it is 30 days.

10-22-2003, 07:31 PM
i never checked when the cookie expires. :(

that is all of the script.

this code isn't going to be used in the 'real world'. its part of a class project, which required javascript to do some 'shopping cart' feature. It just tracks what the user has clicked on.

my goal was to save the 'cart items' in an array, which would be stored in a cookie. Then when i wanted to see the 'cart items' it would just spit the array out using the for() loop. That was where i am now. Unfortunately the class never got into cookies, so i had to 'learn on the way' sort of thing.

I was under the impression js doesn't have a 'session' capability. I also planning to use the DeleteCookie() function to delete a cookie once they 'ordered'.

Either way, i've dumped this js idea and moved to a server side technology with a db backend. But i thought it would be good for me to know what i was doing wrong and why.