...

View Full Version : Setting & Getting Cookies



pinkotoad
07-23-2002, 07:52 PM
I'm trying to make a session only cookie which will transfer the value of a hidden form element onto the next page.
This is my setCookie function


function SetCookie(total1, value)
{
var value = document.form.amount.value
document.cookie = total1 + "=" + escape + (value)
}

getCookie on next page:


function GetCookie(total1)
{
var dcookie = document.cookie;
var cname = total1 + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}

And how I am trying to write it to the document:


document.write(GetCookie("total1"))


Thanks

ShriekForth
07-23-2002, 09:31 PM
I don't see what you are asking, but I am guessing that it is not working, I see at least one problem... escape + (value) escape is a function escape(value).

Although I would make the SetCookie() function not use 'value' as a name of a variable. I don't know if it is officially a reserved word, I would guess so. So something like this will set the cookie.

cookie.html

<script>
function SetCookie(total1, inVal)
{
var newValue = inVal.value
document.cookie = total1 + "=" + escape(newValue)
}
</script>
<form name="temp">
<input type="text" name="tempF" onChange="SetCookie('tempF',this)">
</form>
pass the inVa by reference using "this", or however you want to do it, in that case you create a name value pair tempF=<input>

Then to read it out you have it correct, pass it the name of the name value pair you want to get, in this case 'tempF'. The way you have it will work if the form is named form though.


ShriekForth

pinkotoad
07-24-2002, 12:30 AM
Alright I have been tweaking a bit with the script.
Now the document.write actually writes something, but it is "null"
New SetCookie:


function SetCookie(total1, inVal)
{
var inVal = form.amount.value
document.cookie = total1 + "=" + escape(newValue)
}



<form name="form" onSubmit="SetCookie()">

GetCookie function:


function GetCookie(total1)
{
var dcookie = document.cookie;
var cname = total1 + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}



document.write(GetCookie("total1"))

tamienne
07-24-2002, 06:53 PM
your SetCookie isn't completely correct.

function SetCookie(total1, inVal)
{
var inVal = form.amount.value
document.cookie = total1 + "=" + escape(newValue)
}

you're passing in inVal but then having another local variable called inVal. Get rid of inVal in your parameter list.

also, you're setting your cookie with total1 and newValue. where is newValue coming from? what's the point of inVal if you don't use it?

When you set your cookie onSubmit you didn't pass it a cookie name. you'll want that to retrieve the cookie later.

Here's some code for retrieving your cookie..


function getCookie(cookie_name) {
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;
}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote;
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum