Click to See Complete Forum and Search --> : Need Help With Cookies!!!
Dr1ft3r
12-11-2002, 08:53 PM
hey whats up...i need to know how to write a cookie and then be able to pull out the value later...i have looked all over the internet and all i find are really long and complicated scripts...i hope the base code for a cookie really isnt as long as the ones i have seen...well thanks in advance:D
Dr1ft3r
12-12-2002, 02:47 PM
hey the "setCookie(cookie_name, escape(cookie_value), cookie_expiration);" line was giving me an error in IE. It said "Object Expected" so i came back here and i got your "setCookie(name, value)" function, but i didnt really understand it its a little confusing (im not that great at Javascript yet) and i was wondering if you could explain what each line of code is doing. I always like to know what my code means and if i dont i feel like im stealing it and i dont have a right to use it so if you could just explain that, that would be great...thanks alot for the help
hfraser
12-12-2002, 11:20 PM
a day in the life of a cookie
here is the script
function setMyCookie(myCookieName, myCookieValue, myCookieTime){
// cookie time gotta be in days
var cookieDies = new Date();
// i set the cookie to expire in a week
cookieDies.setTime(cookieDies.getTime() + myCookieTime*24*60*60*1000);
// set the content of the cookie
document.cookie = myCookieName+"="+myCookieValue+";expires=" + cookieDies.toGMTString();
}
function getMyCookie(whatCookie){
var myCookie = new String(document.cookie);
var myCookieHeader = whatCookie+"=";
var myCookieStart = myCookie.indexOf(myCookieHeader);
var myCookieEnd = myCookie.indexOf(';',myCookieStart);
var hans = new String("hanshans");
if (myCookieEnd == -1){
myCookieEnd = myCookie.length;
}
// extract data
// if the cookie does not exist well set one and build your window!
if( myCookie.indexOf(myCookieHeader) != -1){
myCookieStart = myCookieHeader.length;
return myCookie.substring(myCookieStart, myCookieEnd);
}
else{
return false;}
}
to create a cookie
call: setMyCookie(myCookieName, myCookieValue, myCookieTime)
myCookieName = the name of you cookie (has to be between " " or ' ')
myCookieValue = the content of your cookie(has to be between " " or ' ')
myCookieTime = the number of days it will be there
to get the value of you cookie just do
var yourVariable = getMyCookie(whatCookie)
where whatCookie is the name of the cookie that you want to call!
and if you want to eapire your cookie just use the setMyCookie(myCookieName, myCookieValue, myCookieTime) function but instead of puting a number of days put -1
that is the simplest way i can find!
my 2 cents!