title: How to handle cookies in JavaScript?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/260749/what-is-the-best-way-to-get-and-set-a-single-cookie-value-using-javascript">What is the “best” way to get and set a single cookie value using JavaScript</a>  </p>
  </blockquote>
  
  
  
  <p>I am working on a project where it requires to check cookie and tell whether it is 20 minutes old or not. So I have written once code which is like this.
  This is only javascript code I have pasted.</p>
  
  <pre><code>function checkcookie()
  {
      var difftime= getcookie();
      // further operation comes here
  }
  
  var cookieminutes;
  
  function getcookie()
  {
      var start = document.cookie.indexOf("expires");
      var cookiedate;
  
      if(start==-1)
      {
          cookiedate = new Date();
          document.write("Start equal to -1");
          document.cookie="expires="+cookiedate+",path=0,domain=0";
          cookieminutes= cookiedate.getMinutes();
      }
      else
      {
          document.write("Start not equal to -1");
  
          var date =  new Date();
          var minutes = date.getMinutes();
  
          document.write("The difference is "+minutes);
          document.write("&lt;br /&gt;Cookie minutes is "+cookieminutes);
          return (minutes-cookieminutes);
  
      }
  }
  </code></pre>
  
  <p>In function getcookie the variable cookieminutes is coming as undefined. But as I know since it is a global variable it should have the value.</p>
  
  <p>Can anybody please tell what is the solution for this.?</p>