Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

cookies questions



2:18 am on Aug 1, 2009 (gmt 0)

5+ Year Member

Hi i'm having some problems to answer some questions i'm being ask about cookies.

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?



2:56 am on Aug 1, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Try w3schools [w3schools.com...] for starters.

Welcome to forum.


9:51 am on Aug 1, 2009 (gmt 0)

10+ Year Member

Hmm, sounds a lot like home work...



9:30 pm on Aug 1, 2009 (gmt 0)

5+ Year Member

Yes it is homework and i can't do it so i need help.



9:51 am on Aug 2, 2009 (gmt 0)

10+ Year Member

Was daveVk's suggestion any help?


11:53 am on Aug 2, 2009 (gmt 0)

5+ Year Member

Questions 1 & 2 you should be able to determine by a quick google of "javascript cookies" and trying a few things out on your own. I will tackle question #3: the reason it is a good idea (in some cases) to store the date the cookie was set, is that you can not access the cookies expiration date (due to browser security restrictions) after the cookie has been set. Some times you may want your program to know if the users particular cookie is about to expire or not, and the only way to know is to know the date it was set and the duration it was set for (the duration is normally a constant you have pre-determined and won't usually need to be stored in the cookie's value).


4:52 pm on Aug 2, 2009 (gmt 0)

5+ Year Member

Would question 1 answer be something like this?:

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] ) );
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!


12:14 am on Aug 3, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

The previous reference shows the setCookie function, use this 4 times to set 4 name/value pairs.

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.


Featured Threads

Hot Threads This Week

Hot Threads This Month