Connecting Tech Pros Worldwide Forums | Help | Site Map

Cookies?

jojowebdev@gmail.com
Guest
 
Posts: n/a
#1: Jul 14 '06
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.


jojowebdev@gmail.com
Guest
 
Posts: n/a
#2: Jul 14 '06

re: Cookies?


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@gmail.com wrote:
Quote:
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.
jojowebdev@gmail.com
Guest
 
Posts: n/a
#3: Jul 14 '06

re: Cookies?


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?


jojowebdev@gmail.com wrote:
Quote:
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@gmail.com wrote:
Quote:
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.
gimme_this_gimme_that@yahoo.com
Guest
 
Posts: n/a
#4: Jul 14 '06

re: Cookies?


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;
}

Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 14 '06

re: Cookies?


JRS: In article <1152900151.557997.261190@35g2000cwc.googlegroups. com>,
dated Fri, 14 Jul 2006 11:02:31 remote, seen in
news:comp.lang.javascript, gimme_this_gimme_that@yahoo.com posted :
Quote:
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jim Davis
Guest
 
Posts: n/a
#6: Jul 15 '06

re: Cookies?



<jojowebdev@gmail.comwrote in message
news:1152890745.235911.73520@i42g2000cwa.googlegro ups.com...
Quote:
Do javascript cookies REALLY have to be this hard?
Well... yes and no.

"Yes" because all that code you showed is needed: cookies in Javascript can
be very completed (especially to people new to JavaScript) since the
language doesn't abstract their handling. You're left do all the string
parsing, date manipulation and so forth.

"No" because it's actually pretty simple to abstract things for yourself.
Try this:

http://www.depressedpress.com/Conten...kies/Index.cfm

This script will create a cookie managment object which will abstract cookie
handling into method calls to the objects. No global functions are created,
only the single object: "DP_Cookies" (which, itself, does of course have
functions... but you don't need to worry about them as they are encapsulated
into the object).

To set your cookie (after importing this script) you would do:

DP_Cookie.set("whatField", "nbcarrier1");

That would create a session cookie - it will be destroyed when the browser
session ends. To create a cookie that lasts one day you could add:

DP_Cookie.set("whatField", "nbcarrier1", 1);

To change that so the cookie lasts 30 minutes you can do this:

DP_Cookie.set("whatField", "nbcarrier1", 30, "minutes");

To later get the value of the cookie you can do:

var MyCookieValue = DP_Cookies.get("whatField");

To later erase (delete) the cookie do:

DP_Cookies.erase("whatField");

The object also provides methods to easily get all cookies (as a JavaScript
Object) and erase all cookies.

There's full documentation at the link I provided and the object is open
source (under the very liberal BSD license) and free.

I hope it helps,

Jim Davis


Richard Cornford
Guest
 
Posts: n/a
#7: Jul 19 '06

re: Cookies?


gimme_this_gimme_that@yahoo.com wrote:
<snip>
Quote:
// 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.


Closed Thread


Similar JavaScript / Ajax / DHTML bytes