javascript cookies problem

Discussion in 'JavaScript' started by ahdsan, Aug 19, 2009.

  1. #1
    I have a realestate site, a user clicks on a listing and is redirected to listing detail page.
    In the listing detail page there is a form to contact the seller.
    Now,when user clicks on submit, i set javascript cookies , so that if the user comes again he doesn't need to enter details again.
    I went to url
    http://www.xyz.com/listing1.html
    i entered the details , submit , closed the browser and came back, it showed me the values correctly

    Now, if I go to http://www.xyz.com/listing2.html, it has the same form
    it doesn't show the values

    can anyone help me ?


    <script type="text/javascript">
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
    }
    return "";
    }

    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
    </script>

    /////////////////////////////////////////////////////////////
    code to store and retrieve cookies
    /////////////////////////////////////////////////////
    <script type="text/javascript">
    document.myfrm.YMLP2.value=getCookie('YMLP2');
    document.myfrm.YMLP3.value=getCookie('YMLP3');
    document.myfrm.YMLP4.value=getCookie('YMLP4');
    document.myfrm.YMLP5.value=getCookie('YMLP5');
    document.myfrm.YMLP0.value=getCookie('YMLP0');


    function scok()
    {
    setCookie('YMLP2',document.myfrm.YMLP2.value,100);
    setCookie('YMLP3',document.myfrm.YMLP3.value,100);
    setCookie('YMLP4',document.myfrm.YMLP4.value,100);
    setCookie('YMLP5',document.myfrm.YMLP5.value,100);
    setCookie('YMLP0',document.myfrm.YMLP0.value,100);

    document.myfrm.submit();
    }

    </script>
     
    ahdsan, Aug 19, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, I think it doesn't read the same cookie, because you didn't set path or domain where the cookie should work for (now it has for each page one cookie).
    Try to modify function setCookie() :

    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+"; path=/";

    Any better?
     
    lp1051, Aug 19, 2009 IP
  3. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #3
    If both pages are in the same folder, then there's no need to set a path.
    If you could give a URL, this could be fixed straight away.
     
    Logic Ali, Aug 19, 2009 IP