Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Western US
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Delaware, USA
    Posts
    137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    		}
    	}
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •