473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Cookies?

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.

Jul 14 '06 #1
6 2567
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!~

jo********@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.
Jul 14 '06 #2
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?
jo********@gmail.com wrote:
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!~

jo********@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.
Jul 14 '06 #3
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;
}

Jul 14 '06 #4
JRS: In article <11**********************@35g2000cwc.googlegroups. com>,
dated Fri, 14 Jul 2006 11:02:31 remote, seen in
news:comp.lang.javascript, gi*******************@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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 14 '06 #5

<jo********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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
Jul 15 '06 #6
gi*******************@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.
Jul 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
20
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
9
by: | last post by:
Is it possible for a user to enable permanent cookies but disable session cookies.....this seems like a contradition yet this is what I appear to be reading in online articles?
1
by: John Taylor-Johnston | last post by:
I'm a University academic looking for a proper definition of JavaScript Cookies. http://www.CollegeSherbrooke.qc.ca/languesmodernes/604-HAE_Grammar_Practice/ I'm trying to decipher what...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
8
by: CDARS | last post by:
Hi all, I have a confusing question on ASP.NET cookies usage: 1> Response.Cookies("test").value = Now 2> Response.Write(Request.Cookies("test").value) 3> 4> Response.write("<hr>") 5>...
6
by: Stephane | last post by:
Hi, I have a login page where if the user wants his access codes to be saved are set into a cookie. In the logout page, I want to delete those cookies. I tried this and this is not working at...
1
by: Alex Nitulescu | last post by:
I have the following very simple colde (while learning about cookies and session state): Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
by: Claudio | last post by:
I have a demo app where cookies does not work. The first page create a cookies. The second read the contet. If I browse the pages via IE6.0 the pages does not work. If I browse the pages via...
0
by: rn5a | last post by:
This is how I am creating & then reading cookies: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'create cookies Response.Cookies("UserName").Value = "Ron"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.