PDA

View Full Version : How to build cookies client-side so they are handled like Dictionary objects server-s


RadarBob
09-23-2002, 10:19 PM
Before the details of my cookie problem, here's the big picture:
I'm trying to enter multiple sub-records that have a Many:1 relationship to the other data entered on that same web page. I think I need to avoid rebuilding the web page every time the user needs to add another sub-record - I THINK. You tell me if my current solution is unnecessary... and how an alternate solution would look.

My current work-in-progress solution:
Open a child window from the parent page. The sub-record data is kept in a cookie. Using Javascript I can implement the basic "add another sub-record" functionality without having to hit on the server. THE KEY of course is being able to keep the data in cookies. Then, when the main web page is "saved" the cookies data go along to the server as well as the form data.

Now for my cookie problem:
I've hit the 20 cookie limit per server. Each separate field of each record is a separate cookie. Well, I know I could package things so each cookie contained a whole record; I just have to define my own internal delimiters so I can parse each cookie into separate field data ---> doesn't this sound suspiciously like an ASP dictionary object??

"Professional Active Server Pages 2.0 says:
"the cookies collection is implemented in the same way as the dictionary
object."

and further:

".. each member of the cookies collection can hold multiple values ***for that cookie name***" (my emphasis).

We know that cookie names must be unique on the client or else they will overwrite each other. So then, how do I build cookies on the client so they are handled as suggested by the book?

allida77
09-24-2002, 04:02 AM
The dictionary is server side and takes up server resources. I guess what the book means is:


<%
Response.Cookies("myProduct")("Main") = "DevSite"
Response.Cookies("myProduct")("Prd1") = "ASP"
Response.Cookies("myProduct")("Prd2") = "VBScript"
Response.Write Request.Cookies("myProduct")
%>

Output:
PRD2=VBScript&PRD1=ASP&MAIN=DevSite


from
Dev Guru (http://www.devguru.com/Technologies/asp/quickref/response_cookies.html)

I tend to do as much as I can client side so I would use cookies in this manner if possible, for a last resort use the dictionary.

RadarBob
09-25-2002, 01:40 PM
Originally posted by allida77
The dictionary is server side and takes up server resources. I guess what the book means is:


<%
Response.Cookies("myProduct")("Main") = "DevSite"
Response.Cookies("myProduct")("Prd1") = "ASP"
Response.Cookies("myProduct")("Prd2") = "VBScript"
Response.Write Request.Cookies("myProduct")
%>

Output:
PRD2=VBScript&PRD1=ASP&MAIN=DevSite


OK, I don't quite get the output.

First, everything I've read says a cookie consists of four parts: name, equal sign, value, and semi-colon as a separator. That's one cookie. So does the cookie actually look like this:
myProduct=PRD2=VBScript&PRD1=ASP&MAIN=DevSite ; on the client? I assume the value (everything in bold) would be "escaped" to keep any imbedded "=" or ";" from being mis-interpreted.

Again the question is "how do I build the cookie client side so the server side can understand/parse it? Looks like the ampersand "&" is the delimiter I'm looking for. Now that I have this important clue, I'll experiment and let y'all know.


BTW, one book - JavaScript Unleashed says a cookie is delimited by a semi-colon and a space; and all the sample code it shows accounts for the space. Is this "non standard"? Does it really matter?

whammy
09-27-2002, 03:30 AM
The way allida showed it, and as it is in my book, each cookie can have "keys".

When you do this:

Response.Cookies("myProduct")("Main") = "DevSite"

("myProduct") would be the cookie name
("Main") would be the key

Try it out, and then look at the cookie on your hard drive, you'll see how it works (that's what the ampersand does, it separates the cookie key name/value pairs).

I forget how many keys a cookie can have, but that should solve the problem of going over the cookie limit. :D