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 5 of 5

Thread: Cookie Problem

  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Cookie Problem

    Hi,

    I am using the following coding for accessing the cookie.
    dokie = document.cookie
    dokie=unescape(dokie);
    start = dokie.indexOf("name=")+"name=".length;
    end = dokie.indexOf(";", start);
    if(end==-1)end = dokie.length
    document.write(dokie.substring(start, end))

    After entering the username, the username is stored in the cookie and it is accessed in another page as "Welcome Jackson"

    But if i close the ie window again i go to that same url then i am getting as t=2 instead of jackson(username).
    Please Help me.

    Jack

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Heres an example of what I kinda do when I set and Get cookies:
    Code:
    <html>
    <head>
    	 <title>Cookie</title>
    <script language="javascript">
    var expires = new Date();
    expires.setFullYear(expires.getFullYear() + 1);
    function set()
    {
    	var name1 = document.forms[0].firstName.value;
    	document.cookie = encodeURI("fname="+name1) + "; expires=" + expires.toUTCString();
    }
    function getCookie()
    {
    	var cookie = decodeURI(document.cookie);
    	var splitCookie = cookie.split("; ");
    	var name1 = splitCookie[0].split("=")[1];
    	document.getElementById('foo').innerHTML=(name1);
    }
    </script>
    </head>
    <body document.forms[0].firstName.focus();">
    <form>
    First Name <input type="text" name="firstName"><br>
    <input type="button" value="cookie" onclick="set();">
    <input type="button" value="Get Cook" onclick="getCookie();">
    </form>
    <div id="foo"></div>
    </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi,

    Thanx for the sample coding. I can get the out put, but if i close the window and open a new window and goes to the same site instead of accessing the cookie name it displays as t=3. What could be the problem.

    Thanx
    Jack

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    The way you are retreiving your cookie is not persistant. When I run your code I don't get the right results each time. One time I will get the correct results, then the next i'll only get a few letters, and so on. How are you setting your cookie? If you try my example into your code im sure it will work.

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Last edited by Willy Duitt; 12-18-2004 at 04:07 PM.


  •  

    Posting Permissions

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