Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-23-2002, 07:52 PM   PM User | #1
pinkotoad
New Coder

 
Join Date: Jul 2002
Location: Detroit MI
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
pinkotoad is an unknown quantity at this point
Question Setting & Getting Cookies

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
Code:
	function SetCookie(total1, value)
		{
			var value = document.form.amount.value
			document.cookie = total1 + "=" + escape + (value)
		}
getCookie on next page:
Code:
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:
Code:
document.write(GetCookie("total1"))
Thanks
pinkotoad is offline   Reply With Quote
Old 07-23-2002, 09:31 PM   PM User | #2
ShriekForth
Regular Coder

 
Join Date: Jul 2002
Location: Western US
Posts: 169
Thanks: 0
Thanked 0 Times in 0 Posts
ShriekForth is an unknown quantity at this point
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
Code:
<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
ShriekForth is offline   Reply With Quote
Old 07-24-2002, 12:30 AM   PM User | #3
pinkotoad
New Coder

 
Join Date: Jul 2002
Location: Detroit MI
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
pinkotoad is an unknown quantity at this point
Alright I have been tweaking a bit with the script.
Now the document.write actually writes something, but it is "null"
New SetCookie:
Code:
	function SetCookie(total1, inVal)
		{
			var inVal = form.amount.value
			document.cookie = total1 + "=" + escape(newValue)
		}
Code:
<form name="form" onSubmit="SetCookie()">
GetCookie function:
Code:
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;
    }
Code:
document.write(GetCookie("total1"))

Last edited by pinkotoad; 07-24-2002 at 12:55 AM..
pinkotoad is offline   Reply With Quote
Old 07-24-2002, 06:53 PM   PM User | #4
tamienne
Regular Coder

 
Join Date: Jun 2002
Location: Delaware, USA
Posts: 138
Thanks: 0
Thanked 0 Times in 0 Posts
tamienne is an unknown quantity at this point
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..
Code:
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;
		}
	}
}
tamienne is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:32 AM.


Advertisement
Log in to turn off these ads.