Cookie question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Creegan

    #1

    Cookie question

    Below I have a snippet of code for creating a cookie, but am having a
    problem assigning data to it. I pass in values to the function which I
    know are valid, then I assign this info to the cookie using
    document.cookie . But when I try to view the cookie using the alert
    function, the cookie is empty. Any ideas why this might be the case?

    function setCookieValue
    (cookieName,coo kieValue,cookie Path,cookieExpi res)
    {
    cookieValue = escape(cookieVa lue);
    if (cookieExpires == "")
    {
    var nowDate = new Date();
    nowDate.setMont h(nowDate.getMo nth() + 6);
    cookieExpires = nowDate.toGMTSt ring();
    }

    if (cookiePath != "")
    {
    cookiePath = ";Path=" + cookiePath;
    }

    document.cookie = cookieName + "=" + cookieValue + ";expires=" +
    cookieExpires + cookiePath;
    alert(document. cookie);
    }
  • Elus

    #2
    Re: Cookie question

    U¿ytkownik Creegan napisa³:[color=blue]
    > Below I have a snippet of code for creating a cookie, but am having a
    > problem assigning data to it. I pass in values to the function which I
    > know are valid, then I assign this info to the cookie using
    > document.cookie . But when I try to view the cookie using the alert
    > function, the cookie is empty. Any ideas why this might be the case?
    >
    > function setCookieValue
    > (cookieName,coo kieValue,cookie Path,cookieExpi res)
    > {
    > cookieValue = escape(cookieVa lue);
    > if (cookieExpires == "")
    > {
    > var nowDate = new Date();
    > nowDate.setMont h(nowDate.getMo nth() + 6);
    > cookieExpires = nowDate.toGMTSt ring();
    > }
    >
    > if (cookiePath != "")
    > {
    > cookiePath = ";Path=" + cookiePath;
    > }
    >
    > document.cookie = cookieName + "=" + cookieValue + ";expires=" +
    > cookieExpires + cookiePath;
    > alert(document. cookie);
    > }[/color]

    There's many mistakes in your script. Why don't you use this one:


    ....or my own ;)
    function sCookie(N,V,D){
    document.cookie =N+'='+escape(V )+(D?'; expires='+new Date(+new
    Date()+D*864e5) .toGMTString(): '')+'; path=/'
    }

    Comment

    Working...