/    Sign up×
Community /Pin to ProfileBookmark

Script using cookies

I’ve been stuck with this script for almost 2 weeks.

I need to write a script that use cookies to keep in memory the user’s preferences. First, a form to collect data. I need to know the name and second name of the user and his favorite color. After that, I need to valid all the values of the form, that is to say to be sure that they’re not empty and that the color is a defined color. The form will be displayed at the first visit of the user then it will greet the user with a welcome message. Moreover, the background color will match the color that the user chose. The welcome message needs to be as follow: “Hi, Name secondName, Good morning! (if time is between 6am and 12), Good afternoon! (if time is between 12 and 6pm) or Good evening! (Between 6pm and 6am).

Thank you sooooo much!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeJan 25.2011 — Start off by checking to see if the cookie exists

if it does, skip straight to loading the page customized by values in the cookie

if it doesn't, get the current hour/AM-PM and set to a variable

base greeting upon hour

prompt dialog asking for first name, last name, favourite colour

save values into a variable - place cookie with values

Check for cookie, get values, load page.

^_^
Copy linkTweet thisAlerts:
@wbportJan 25.2011 — I use this to read and write cookies[CODE]function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}[/CODE]
The following reads them [CODE]function startup() {
var crumbs = new Array()
var retCookie = getCookie("WPGuiChord");
if (retCookie.length > 10) { // Cookie was valid
crumbs = retCookie.split("|");
document.nwcform.NoteDur.selectedIndex = crumbs[0];
document.nwcform.bIsDot.checked = crumbs[1];
document.nwcform.A1[crumbs[2]].checked = true;
...
document.nwcform.namCrdC.checked = crumbs[7];
document.nwcform.namCrdPos.value = crumbs[8]; }
else { //No or invalid cookie
document.nwcform.selAll.checked = true;
document.nwcform.namCrdC.checked = true;
document.nwcform.A1[1].checked = true; }[/CODE]
while this writes them:[CODE] my_cookie += document.nwcform.FQUser.selectedIndex + "|";
my_cookie += ((document.nwcform.namCrdC.checked) ? 1 : 0 ) + "|";
my_cookie += document.nwcform.namCrdPos.value;
setCookie("WPGuiChord",my_cookie,91); //Set for three months[/CODE]
The "|" is used to separate the cookie value into individual fields.
×

Success!

Help @jls1988 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...