Cookies?

J

jojowebdev

Do javascript cookies REALLY have to be this hard?

function setCookie( name, value, expires, path, domain, secure ) {
var today = new Date();
today.setTime( today.getTime() );
if ( expires ) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name+"="+escape( value ) +
( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) +
//expires.toGMTString()
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


I ONLY want a cookie that says a field name is nbcarrier1 for example.
Nothing else.
I want to call the cookie whatField.

I will hold it and squeeze it and love it and... eat the cookie.

I just want a simple cookie. I don't WANT it in its own function. I
already have a function.

Please help.
 
J

jojowebdev

Yay! I found a way to set my cookie.

document.cookie = 'whichField=' + escape(formEl.name) +';'

How do I "read" the cookie just as easy?

I tried this but it did not work:

var eatCookie = document.cookie("whichField");
alert(eatCookie);


Help appreciated!~
 
J

jojowebdev

How do I delete my cookie I made with:
document.cookie = 'whichField=' + escape(formEl.name) +';'

I want to do it with the same kind of code.
Do I create it again with a negative date in order to delete it?
 
G

gimme_this_gimme_that

Your milage may vary ...

Example sets and fetches a cookie named SGDDName from a form variable
named sggdd_name

// store data into cookies.
function setCookies(form) {
var expires = new Date();
var oneyear = expires.getTime() + (365*24*60*60*1000);
expires.setTime(oneyear);
var Name_value = form.sgdd_name.value;
document.cookie = "SGDDName=" + Name_value + ";expires=" +
expires.toGMTString();
}


// parses a sting with lots of data. Returns human readable contents.
function getCookieData(name) {
var arg = name + "=";
alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while ( i < clen ) {
var j = i + alen;
if ( arg == document.cookie.substring(i,j) )
return getCookieVal(j);
i = document.cookie.indexOf(" ", i ) + 1;
if ( 0 == i ) break;
}
return null;
}

// getCookieData helper function
function getCookieVal(offset ) {
var endstr = document.cookie.indexOf(";", offset);
if ( -1 == endstr )
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}


function setValues(form) {
// loads values into form
// called at bottom of html page
name_entered = getCookieData("SGDDName");
if (! name_entered ) name_entered = "";
form.sgdd_name.value = name_entered;
}
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 14 Jul 2006 11:02:31 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
var expires = new Date();
var oneyear = expires.getTime() + (365*24*60*60*1000);
expires.setTime(oneyear);

ISTM that either you're using an old book or you have a low-grade
lecturer. The following is shorter and will give a full civil year.

var expires = new Date()
expires.setFullYear(expires.getFullYear()+1)

Read the newsgroup FAQ.
 
R

Richard Cornford

// getCookieData helper function
function getCookieVal(offset ) {
var endstr = document.cookie.indexOf(";", offset);
<snip>

Internet security/desktop firewalls often operate as content
inserting/re-writing proxies and with privacy settings set to
restrict/prevent cookie use they often re-write the property accessor -
document.cookies - in such a way that it resolves as an undefined value.
As a result and code (such as the above) that acts as if -
document.cookies - will resolve as a string value will error-out
whenever such a security/firewall program is operating on the client.

This is an easy condition to miss in testing but a long history of
problems posted to this group truing out to be caused by this phenomenon
demonstrates that it is a reality. The erroring out can easily be
avoided by adding a test for the nature of the result of -
document.cookies - prior to treating it as a string.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top