www.webdeveloper.com
Results 1 to 2 of 2

Thread: JS Cookie getvalue

  1. #1
    Join Date
    Nov 2007
    Posts
    5

    JS Cookie getvalue

    I have a few javascript functions bellow
    Code:
    <script type="text/javascript">
    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());
    }
    
    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 "";
    }
    </script>
    <script type="text/javascript">
       function cookieWhatWeSay(name)
       {
          cookie=getCookie(name);
          if (cookie!=null)
          {
             var value = cookie.GetCookieValue();
             if (value == "inline")
             {
                document.getElementById('whatwesay1').style.display = 'inline';
                document.getElementById('whatwesay2').style.display = 'none';
                setCookie('whatwesay','inline',365);
             }
             else
             {
                document.getElementById('whatwesay1').style.display = 'none';
                document.getElementById('whatwesay2').style.display = 'inline';
                setCookie('whatwesay','none',365);
             }
          }
       }
    </script>
    
    </head>
    <body onLoad="cookieWhatWeSay('whatwesay')">
    The setCookie function works perfectly but the one I am interesetd in is cookieWhatWeSay(name) this works partially I need to know how I can get the value from the cookie. Basically the cookie can have two values (i.e. inline or none) How do I get this value from the cookie and then use the if statements in the cookieWhatWeSay(name) function.

    Hope you understand what I say

    Please help.

    Cheers - Zahid

  2. #2
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    first: the way you get the cookie is wrong.
    Code:
             var value = cookie.GetCookieValue();
    here cookie is a string that already contain the value of the retrieved cookie. unless GetCookieValue is a prototyped function for String, then is ok.

    second: the following condition make no sense. when the cookie doesn't exist getCookie function return an empty string. which in turn, an empty string IS NEVER the same as null. in this case, the evaluation is always true.
    Code:
          if (cookie!=null)
          ...
          }
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles