Forum Moderators: open
Cookies gives me headache.
Anybody can help?
Here's how it goes:
I want to store four cookies -- the name, the date the cookie was set, and the user's preferences
for background color and foreground color. The name is set to John, the date set to 03-28-2005,
the background color is set to red, and the foreground color is set to green.
Question 1:
What is the entire cookie string (or name-value pair) returned when John visits the webpage?
Question 2:
What code to use to extract the four values from the cookie string? The code should be written
in a way that these values can be extracted wathever the order they appear in the cookie string.
Question 3:
Why is it a good idea to store the date that the cookie is set? How is this information be useful?
Thanks!
document.cookie = "name=John; setTime=03-28-2005; bgColor=red; fgcolor=green;"
And if there is only 1 cookie string why di i need 4 cookies? Isn't 4 properties of the same cookie?
Regarding question 2 would it be something like this:?
function get_cookie ( cookie_name )
{
var results = document.cookie.match ( '(^¦;) ?' + cookie_name + '=([^;]*)(;¦$)' );
if ( results )
return ( unescape ( results[2] ) );
else
return null;
}
All i would need to do is to add the colors and date parametres?
I don't know how to do this (question 2..), cookies are a pain :(
Thanks for your help!
Note that document.cookie does not directly represent what is stored in cookie, a read of document.cookie will not return what is written to document.cookie, document.cookie = ... adds to cookie.
What is wrong with the method of retrieving cookie show in reference ? Try your solution using 'try it' link, this should give you good idea of how it works.