/    Sign up×
Community /Pin to ProfileBookmark

BakeCookie/ EatCookie- trouble making a cookie

thread reference:
[url]http://www.webdeveloper.com/forum/showthread.php?s=&threadid=39473&highlight=BakeCookie[/url]

Please bear with me, I’m still trying to learn Javascript.
I’ve been trying to figure out the BakeCookie and EatCookie.
I’ve ran several tests and I’m doing something wrong, I can’t seem to find out what, but it is not placing a cookie.

[SIZE=1]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2//EN”>
<html>
<head>
<SCRIPT type=”text/javascript”>
function BakeCookie(name,value) {
var argv=arguments;
var argc=arguments.length;
var expires=(argc>2) ? argv[2] : null;
var path=(argc>3) ? argv[3] : null;
var domain=(argc>4) ? argv[4] : null;
var secure=(argc>5) ? argv[5] : false;
document.cookie=name+”=”+escape(value) +
((expires === null) ? “” : (“; expires=”+expires.toUTCString())) +
((path === null) ? “” : (“; path=”+path)) +
((domain === null) ? “” : (“; domain=”+domain)) +
((secure === true) ? “; secure” : “”);
}
</SCRIPT>
<SCRIPT type=”text/javascript”>
function EatCookie(name) {
var arg=name+”=”;
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j) == arg) {
return EatCookieVal(j);
}
i=document.cookie.indexOf(” “,i) + 1;
if (i === 0) {break;}
}
}
function EatCookieVal(offset) {
var endstr=document.cookie.indexOf(“;”,offset);
if (endstr == -1) {endstr=document.cookie.length;}
return unescape(document.cookie.substring(offset,endstr));
}
</SCRIPT>

</head>
<body>
<form>
<!– make text box to enter name and store it in a cookie –>
<INPUT name=”cookiestorage01″ type=”text” value=”enter a name” size=”30″><br>
<INPUT name=”cookievalue” type=”text” value=”cookie value” size=”30″>
<INPUT type=”button” value=”Set Cookie” onClick=”BakeCookie(cookiestorage01,cookievalue);”>

</form>
</body>
</html>
[/SIZE]

The only part of the code that I did was in the Form. The rest of it was copied directly from the BakeCookie/EatCookie code.
I’m not doing so well with the document.write command, I was trying to start with some basics first.

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Aug 06.2004 — &lt;INPUT type="button" value="Set Cookie" onClick="BakeCookie(cookiestorage01,cookievalue);"&gt;What are you trying to do here? If you answer this the modifications will be very simple.

... it seems you wnat to store the value of cookievalue text box with the name of the cookiestorage textbox. If yes, then add: form. , before each cookie... and .value after each.

Dr. Script
Copy linkTweet thisAlerts:
@anothenauthorAug 06.2004 — Do I have the format correct?

If I understand it correctly, I need two variables, one for the name of the cookie, the other for the value.

cookiestorage01 is my variable for the name, and cookievalue is the value, the contents that I want stored in the cookie.

Am I understanding it correctly?
Copy linkTweet thisAlerts:
@steelersfan88Aug 06.2004 — If I understand correctly, change your button to:&lt;INPUT type="button" value="Set Cookie" onClick="BakeCookie(this.form.cookiestorage01.value,this.form.cookievalue.value);"&gt;This could become a submit button to te form, and then you would move the onclick="..." (... = stuff between ""s) to the form tag as onsubmit="...;return false;" . either way you'd get the results you need.

Dr. Script
Copy linkTweet thisAlerts:
@steelersfan88Aug 06.2004 — btw, I think we both understand correctly ?
Copy linkTweet thisAlerts:
@LunpaAug 07.2004 — Intresting...

Anyhow, I -too- am confused with cookies.

What I can gather from the other thread is

document.cookie = "Username = " + document.myForm.username.value + "Password = " + document.myForm.password.value;

this usefull snibbit of info... for writing the cookie, atleast.


my guess is that in this method it writes an array to the same variable, but it all looks like a string to me.

"username = booskaboo password = somethinganother" I would guess could be [b]a[/b] possible outcome of saving the string, but to read the cookie, do I have to parse it, or is there a better way?
Copy linkTweet thisAlerts:
@anothenauthorAug 07.2004 — Aha, there's some more code in there. Now this I don't understand too well. I'll place the new code in there and try it. Thank you.

Now, can you help me to understand what the "this" and the "form" is for? I can about guess, "this" means this value and "form" means the current form?

[SIZE=3](this.form.cookiestorage01.value,this.form.cookievalue.value)[/SIZE]

----------------- edited, to avoid double post -----------

Okay I tried it with no success. I did make a few changes to the variables to help me understand:

[size=1]

<form>

<!-- make text box to enter name and store in in a cookie -->

<INPUT name="cookiename01" type="text" value="enter a name" size="30"><br>

<INPUT name="cookievalue" type="text" value="cookie value" size="30">

<INPUT type="button" value="Set Cookie" onClick="BakeCookie(this.form.cookiename01.value,this.form.cookievalue.value);">

<!-- <INPUT type="button" value="Set Cookie" onClick="BakeCookie(cookiename01,cookievalue);"> -->



</form>
[/quote]
[/size]
------------------ edited ----------------

Just a thought, is there something special I need to do the the <form> command? I've noticed sometimes in Forms, they are named.

------------------- edit -----------------

One last thing. Is the "this" of "(this.form. ... " a Javascript reserved word? I'm sure it is, I just want to make sure.
Copy linkTweet thisAlerts:
@steelersfan88Aug 07.2004 — Yes, this is a reserved word, it refers to the current object. In this case, this is the button. this.form refers to the whole form in which the button is located. this.form.[i]whatever[/i] refers to the element named [i]whatever[/i] in the form of the button. the .value just grabs the value of that field.

What errors are you getting?

The form tag would be if we used a submit button. We don't want to submit the form, so we don't need to do that.
Copy linkTweet thisAlerts:
@Mr_JAug 07.2004 — I have some examples of creating cookies on my site if you would like to take a look.

www.huntingground.freeserve.co.uk/scripts/cookies/the_cookie.htm
Copy linkTweet thisAlerts:
@anothenauthorAug 08.2004 — My last reply didn't post, I don't remember what I said.

Thanks MR J, I'll check it out later.

Steelersfan, I'm gonna set the BakeCookie aside for a bit until I get a better grasp on cookies. I went back to the basics and used the code from "HTML Goodies", modified it a bit and here's what I came up with:
[size=1]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html><head>



<!-- file name is "cookie_generic.html" -->



<!-- Variables to be changed: Cookiedata_ , Entered_, putCookie_, formname_, variable_, -->



<SCRIPT LANGUAGE="JavaScript">cookie_name = "Cookiedata_"; var Entered_; function putCookie_() {if(document.cookie != document.cookie){index = document.cookie.indexOf(cookie_name);} else { index = -1;} if (index == -1) {Entered_=document.formname_.variable_.value; document.cookie=cookie_name+"="+Entered_+"; expires=Monday, 04-Apr-2010 05:00:00 GMT";}}</SCRIPT>



<!-- variables to be changed: youwrote_, getname_ -->



<SCRIPT LANGUAGE="JavaScript"> cookie_name = "Cookiedata_"; var Retrieved_; function getname_() {if(document.cookie){index = document.cookie.indexOf(cookie_name); if (index != -1){namestart = (document.cookie.indexOf("=", index) + 1); nameend = document.cookie.indexOf(";", index); if (nameend == -1) {nameend = document.cookie.length;} Retrieved_ = document.cookie.substring(namestart, nameend); return Retrieved_;}}} Retrieved_=getname_(); if (Retrieved_ == "Cookiedata_") {Retrieved_ = "Nothing_Entered"}</SCRIPT>



</head>

<body>



<FORM NAME="formname_">



Enter A : <INPUT TYPE="text" NAME="variable_" size="20" VALUE=" Word "onBlur="putCookie_()"> here <br>



<a href="cookie_generic.html"> Continue </a><br>



<SCRIPT LANGUAGE="javascript"> document.write("You Entered " +Retrieved_+ "."); </SCRIPT>



</FORM>



</body>

</html>[/size]
[/quote]

Although the code may be drawn out more than needed, but I need to understand it first. Once I do, I'll come back to BakeCookie.

Thanks
Copy linkTweet thisAlerts:
@LunpaAug 08.2004 — That seems rather hairy, though.


I would like to know about the very inner workings of cookie commands and varriables themselves, so I can apply it efficiently to my projects.

A hairy, all-perpose cookie handling function that can be put into every situation imagineable is inferior to a single-perpose function that only does EXACTLY what you need it to, and nothing more.
Copy linkTweet thisAlerts:
@Mr_JAug 09.2004 — The setCookie, getCookie, and deleteCookie are standard functions that will do whatever you tell them to, so if you are wanting to be able to save every conceivable saving senario I say good luck to you.

I will watch out for that script
Copy linkTweet thisAlerts:
@anothenauthorSep 14.2004 — I'm back to work on the cookie project now.

Mr J. for some reason your site is returning errors and It takes a very long time to load.

It seems I need an example of the BakeCookie() and EatCookie(). The site that offers it does not provide one.

I tried the suggestion with no results.

My goal is to take a few variables from one page and use the contents on another page.
Copy linkTweet thisAlerts:
@Mr_JSep 14.2004 — Mr J. for some reason your site is returning errors and It takes a very long time to load.[/QUOTE]

I went to take a look earlier today and you were right it was a bit dodgy even on my broadband connection.

Just been again tonight and everything seems back to normal so I can only put it down to some sort of server problem.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...