/    Sign up×
Community /Pin to ProfileBookmark

script that creates a cookie

? I need a javascript application that creates a cookie for daily quote. I have code for the daily quote but don’t know how to make it in to a cookie. Any help will be greatly appreciated.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@ricpApr 29.2007 — simply google "javascript getcookie setCookie" and you'll get tons of hits for a set of functions that seem to be as old as the language itself. However even though they are old they are the "de-facto" cookie scripts around the place.
Copy linkTweet thisAlerts:
@digitalentropyApr 29.2007 — For a more refinded search you can use response.Cookies & request.cookies

I suggest using www.w3schools.com...

I just used their site last night to add cookies functionality to my website.



I hope that helped because I've done nothing but ask questions on this site and finally I can answer one.
Copy linkTweet thisAlerts:
@ricpApr 29.2007 — 
I suggest using www.w3schools.com...

I just used their site last night to add cookies functionality to my website.
[/QUOTE]


http://www.w3schools.com/js/js_cookies.asp

They have used the classic get/set cookie code I mentioned in my post although for some reason removed the domain and path properties from them. I guess whoever wrote that page didn't understand what they did and thought removing them would make no difference.
Copy linkTweet thisAlerts:
@digitalentropyApr 29.2007 — http://www.w3schools.com/js/js_cookies.asp

They have used the classic get/set cookie code I mentioned in my post although for some reason removed the domain and path properties from them. I guess whoever wrote that page didn't understand what they did and thought removing them would make no difference.[/QUOTE]



Please explain? I just followed the w3schools.com tutorial and it seemed to work fine. The cookie is only valid for the session and subsequent pages correctly recognized the cookie and allowed entry.

What are these domain and path properties you're talking about?
Copy linkTweet thisAlerts:
@ricpApr 29.2007 — Netscape "invented" the cookie, you can read their spec here:

http://wp.netscape.com/newsref/std/cookie_spec.html

Yes, it is a very old document, but cookies haven't changed since they were implemented.
Copy linkTweet thisAlerts:
@digitalentropyApr 29.2007 — Netscape "invented" the cookie, you can read their spec here:

http://wp.netscape.com/newsref/std/cookie_spec.html

Yes, it is a very old document, but cookies haven't changed since they were implemented.[/QUOTE]


From my understanding, though, the domain and path are not required and it seems that the default values for those will work fine in my case.

so, domain being the domain of my website (www.congs.org)

and path being the path in which the file is located (/NSEC)

...or atleast this is how I understood that linked page in your last response. Am I correct in assuming that I do not need to actually set the domain and path because the default values are the ones I would set anyway?
Copy linkTweet thisAlerts:
@ricpApr 29.2007 — They are optional, and I am not saying the code on w3schools is wrong, it's just incomplete. For 90% of all your cookie tasks I am sure that it would work perfectly for the job needing done.

The question really should be asked, "why did the contributor to the site consider it necessary to remove that functionality?" It's not like it was overly bloated or redundant in any way.
Copy linkTweet thisAlerts:
@digitalentropyApr 29.2007 — They are optional, and I am not saying the code on w3schools is wrong, it's just incomplete. For 90% of all your cookie tasks I am sure that it would work perfectly for the job needing done.

The question really should be asked, "why did the contributor to the site consider it necessary to remove that functionality?" It's not like it was overly bloated or redundant in any way.[/QUOTE]



Good point. Your observation makes me question the other code snippets I've found on their site.

Thank you for the quick reply and help. I hope this helped the original poster, too.
Copy linkTweet thisAlerts:
@ricpApr 29.2007 — Good point. Your observation makes me question the other code snippets I've found on their site.
[/QUOTE]

Thanks. ?

However the sad fact is you should question [I]all[/I] code snippets found on the Internet! There are some absolute nightmares out there, code so bad it could bring a tear to a glass eye.

Mine is excellent of course, it goes without saying.. :rolleyes:
Copy linkTweet thisAlerts:
@highspeed4evaauthorApr 30.2007 — For a more refinded search you can use response.Cookies & request.cookies

I suggest using www.w3schools.com...

I just used their site last night to add cookies functionality to my website.



I hope that helped because I've done nothing but ask questions on this site and finally I can answer one.[/QUOTE]


Thank you for your help, but I still can't figure out how to do it. How do I make it in to a cookie? I don't know what I'm missing, I am completely lost on this one. This is the code I have for the quote of the day script.

<script language=javaScript>

var messages=Array();

function newMessage(mydate,mytext){

messages[messages.length]={

date:mydate,

text:mytext

}

}



function showMessage(){

var len=messages.length;

var d=new Date();

var mytext="";

var dt=null;

for(i=0;i<len;i++){

dt=new Date(messages[i].date);

if((d.getMonth()==dt.getMonth()) && (d.getDate()==dt.getDate()) && (d.getFullYear()==dt.getFullYear())){

var msg=messages[i];

mytext+="Quote of the day:";

mytext+=msg.text;

break;

}


}

return mytext;

}



newMessage("4/30/2007","Army Strong!");

newMessage("5/1/2007","Army of one!");

newMessage("5/2/2007","Be all you can be!");

newMessage("5/3/2007","Enjoy your visit!");

newMessage("5/4/2007","Always first!");

newMessage("5/5/2007","Keep it moving!");


alert(showMessage());

</script>

Any help will be greatly appreciated for I am completely lost.
Copy linkTweet thisAlerts:
@digitalentropyApr 30.2007 — response.cookies("visitor")="visited today"

response.cookies("visitor").Expires =

'you need to figure out how to use the Date() function to set the line directly above. This will make the cookie expire at the end of the current day, which will make the cookie equal nothing the following days.




if response.cookies("visitor") <> ""{

'the visitor was already here today and the value is set to "visited today"

}

else{

'the visitor hasn't been here today

}




at least, this is how I understand cookies. I don't set the expiration and therefore once the user closes out their session the cookie is emptied/deleted/useless.
×

Success!

Help @highspeed4eva spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...