title: Accessing cookies in javascript
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1454619/read-cookie-in-javascript">Read cookie in javascript</a>  </p>
  </blockquote>
  
  
  
  <p>I have written the following function.  I need to access cookies using the format <code>$COOKIE['cookieName']</code>.</p>
  
  <pre><code>var $COOKIE = (function(){
      if(!document.cookie)
          return {};
      else{
          var c = document.cookie.split(';');
          var len = c.length;
  
          var ret = {};
          var temp ;
  
          for( var i = 0 ; i &lt; len ; ++i )
          {
              temp = c[i].split('=');
  
              ret[temp[0]] = temp[1];
          }
  
          return ret;
      }
  })();
  </code></pre>
  
  <p>I have set two cookies a1= 2 , a2 = 9.  <code>$COOKIE['a1']</code> gives me 2, but <code>$COOKIE['a2']</code> is undefined.</p>
  
  <p>Why is it happening?  What is the problem in my logic/code?</p>