From: jojowebdev on
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!~

jojowebdev(a)gmail.com wrote:
> 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.

From: gimme_this_gimme_that on
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;
}

From: Dr John Stockton on
JRS: In article <1152900151.557997.261190(a)35g2000cwc.googlegroups.com>,
dated Fri, 14 Jul 2006 11:02:31 remote, seen in
news:comp.lang.javascript, gimme_this_gimme_that(a)yahoo.com 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.

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
From: Richard Cornford on
gimme_this_gimme_that(a)yahoo.com wrote:
<snip>
> // 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.


 | 
Pages: 1
Prev: Changing label
Next: Delete cookie?