homepage Welcome to WebmasterWorld Guest from 54.204.231.253
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
cookies questions
thegrinder




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

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?

Thanks!

 

daveVk




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

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

Welcome to forum.

Arno_Adams




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

Hmm, sounds a lot like home work...

Tom

thegrinder




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

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

Cheers

Arno_Adams




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

Was daveVk's suggestion any help?

astupidname




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

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).

thegrinder




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

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

daveVk




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved