cookie chaos
First off i must say that using cookies didn't turn out as i expected
They were MUCH easier than i thought they would be and they weren't as advanced as i thought(see below)
Anyway my problem is i am using cookies for a nickname, and a 'last visited' cookie for my tutorials. and everytime i test my page where its supposed to document.write the chosen nickname it says 34 (which i discovered to be the amount of visits) isn't there a way to give the cookies a name when SAVING them so that there wouldn't be as much chaos when im trying to load them later.
or can you at least figure out an answer to my problem? here's my code its sort of long:
function checkCookie() {
// ===================================nickname===================================
// ------------------------------------------------------create cookies----------------------------------------------
// checks if amount of cookies is 0(no cookies) if yes then create one
if (document.cookie == "") {
// ask for name
var the_name = prompt("Please choose a nickname:","");
// Get current date and put into variable expireDate
expireDate = new Date
// adds 12 months to current date making the cookie's expiration date a year from now
expireDate.setMonth(expireDate.getMonth()+12)
// sets the stuff in quotes to variable the_cookie
var the_cookie = "username=" + the_name + ";expires=" +expireDate.toGMTString()
// tells browser to save above the_cookie as a cookie
document.cookie = the_cookie
// set current page to variable thisPage
thisPage = location
/* refresh page just in case where the name was supposed to be it said undefined(since the page will load before the cookie is completed)*/
window.location = thisPage
// below are a few comments on how to call this cookie
}
/*
use the above code to alert the user about their name(useless, but you get the idea)
*/
}
// ==================================Last visit==================================
// * Under Construction *
// -----------------------------------------------------Read Cookies-----------------------------------------------
// checks if amount of cookies is NOT 0(there are cookies) if yes then read them
if (document.cookie != "") {
// sets the saved cookie to the variable the_cookie
var the_cookie = document.cookie;
// splits cookie into two parts, before and after colon and sets it to the variable broken_cookie
var broken_cookie = the_cookie.split("=");
// sets second peice from split cookie above to the variable the_name
var the_name = broken_cookie[1];
// replaces 20% with space
var the_name = unescape(the_name);
}
// if there are no cookies, set the_name(variable for cookie as used above) to null(so as not to cause an error) until the cookie is created
else if (document.cookie == "") {
the_name = ""
}
i hope that anyone can answer my questions becuase i can't(obviously, or i wouldn't be typing this).
Ian,
---
haha smilies:
hehe
Mark Hensler posted this at 06:32 — 14th November 2000.
He has: 4,048 posts
Joined: Aug 2000
I'm guessing that your creating a counter cookie as well (cause that's the value your reading), you didn't include it in the code, that's why I ask.
Anyway, you'll eventually be using multiple cookies. I dug this out of a book..
//pass the name of the cookie that you want to retrieve the value for
function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i=0; i<thisCookie.length; i++) {
if (cookieName == thisCookie[i].split("=")[0]) {
return thisCookie[i].split("=")[1]
}
}
return 0
}
This will loop through all your cookies looking for the cookieName that you passed it. When it finds the cookie, it will return the value.
Good Luck,
Mark Hensler
If there is no answer on Google, then there is no question.
Vincent Puglia posted this at 01:58 — 15th November 2000.
They have: 634 posts
Joined: Dec 1999
Hi Ian,
Cookies are nice, aren't they?
Any rate: while Max's solution will work, it won't help cutting down the code, especially if you decide to add more cookies. An alternative would be to put all of your information into a pseudo array and save it as the cookie. Then when you retrieve the cookie, you simply split the array and retrieve your individual values.
var cookieData = "";
cookieData = prompt(blahblah) ;
cookieData = "|" + lastVisitCode;
var the_cookie = cookieData + ";expires=" +expireDate.toGMTString()
when you retrieve it, you simply split the string into an array which you then assign appropriately:
var tmpArray = cookieData.split("|");
var the_name = tmpArray[0];
var lastVisit = tmpArray[1];
...etc...
Hope this helps
Vinny
Where the world once stood
the blades of grass cut me still
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.