[thelist] JS Cookie problem - more info!

Keith cache at dowebscentral.com
Mon Jan 13 12:47:07 CST 2003


nagrom said

>now the problem is, you can't store complex datatypes as cookies.
>this is what session variables in server side languages are commonly used
>for....


The only limitation to what can be stored in a cookie is the size of the
cookie, the complexity of data is not really a limiting factor. Take the cookie

item1=3|29.49|A1 Widget|2.44^3.61^491|1|7.25

in the format

item# = qty | price | name | oxen^train^pigeon | taxable | taxrate

to use:
  javascript:getCookieData("item")

strng='<table>'
strng+='<tr><td>Qty</td><td>Desc</td>'
strng=+'<td>Price</td><td>Ext</td></tr>'

function getCookieData(obj){
cookies=document.cookie.split(";");
         for(i=0;i<cookies.length;++i){
                 if(cookies[i].indexOf(obj) != -1){
                         thisCookie=cookies[i].split("=")
                 params=thisCookie[1].split("|");
                 doStrng(params)
                 }
         }
strng+="</table>"
document.getElementById("shoppingcart").innerHTML=strng
}

function doStrng(params){
ext=FPfix(params[0]*params[1])
strng+="<tr><td>"+params[0]+"</td><td>"+params[2]
strng+="</td><td>"+params[1]+"</td><td>"+ext+"</td></tr>"
}

function FPfix(obj){
obj=obj*100;obj=Math.round(obj);obj=obj.toString();
obj=obj.substring(0,obj.length-2)+"."+
obj.substring(obj.length-2,obj.length)
return obj;
}

The above simply prints a shopping cart listing the items, quantities,
prices and item extensions, but the params are all there to finish off with
a subtotal, shipping, tax and total line too.  The shipping parameter is
just another dimension array with it's own set of parameters.

But I agree with jeff - it's time to array the cookie jar...

Keith
====================
cache at dowebscentral.com




More information about the thelist mailing list