PDA

View Full Version : Deletion of document.cookie elements, need help.


Wushu
12-07-2005, 01:51 AM
Hiya, ive stored lots of dvd titles in document.cookie and they are displaying as

"dvds=%3AIndiana%20Jones%20Box%20Set%203AStar%20Wars%20Episode%20III"

I have two pages, a buy page and a cart page. In the cart page it transfers all the data of document.cookie into an array and spits it out to display. Im able to delete the titles from cookiearray which holds the dvds on the cart page but document.cookie is still holding those titles so each time i go to the page it rebuilds the old cart.

Ive been trying to implement this piece of code but i can seem to work it out, the spaces are giving me problems. - this is part of an onclick function which removes items from the cart. I can do it with the array no problem i just cant seem to work out how to do it with document.cookie, this method works when this are no spaces involved.




str = cookiearray[counter+1]

document.cookie = document.cookie.replace(':' + str, "");



Any ideas?

PhotoJoe47
12-07-2005, 05:19 AM
I see you have not given up on session cookie for your shopping cart.

Here is a link that should explain a lot about session cookies.

http://www.javascriptkit.com/javatutors/cookie2.shtml

On that page they will point you to another page that has a good set of prebuilt functions for dealing with session cookies. I think you will find them helpful.

From what I now (thanks to you) understand about session cookies, yours could be just one of many that are in the object "document.cookie". Each cookie is delimited by a ";". Since document.cookie does not like "space" and some other special charcters you need to use the escape() function when you are writting your string to the document.cookie object and use the unescape() function when reading your cookie from the document.cookie.

After your delete function you need to rebuilt your cookie from sratch. Because you can not remove just a part of the value of your cookie, but you can replace it by building it again with only the new complete value.

newcookie = "dvds=:Indiana Jones Box Set :Star Wars Episode III"
document.cookie = escape(newcookie)

dvds is the name of your cookie and the stuff after the "=" sign is the value of your cookie.

Also do you know that the user has to have cookies enabled on their browser for you to use this don't you?

Wushu
12-07-2005, 01:52 PM
Spent alot of time trying to do it but was unsuccessful, i will most likely continue this problem after friday. - I have alot of vb.net to do aswell as some interface stuff. It unfortunate i have to leave it so close to the end but, in the end, it was my own inexperience that lead to this.

Thanks for the help on this joe.

Oh and btw, apparently document.cookie wont just hold 1 cookie session.... its something like that i found out yesterday. Very odd, so im going to try stear clear of it in the future.