title: JavaScript cookie misconceptions
body: <p>I'm using the following code for setting/getting deleting cookies:</p>
  
  <pre><code>function get_cookie(cookie_name)
  {
      var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
  
      if (results)
          return ( decodeURI(results[2]) );
      else
          return null;
  }
  
  
  function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure)
  {
      var cookie_string = name + "=" + encodeURI(value);
  
      if (exp_y)
      {
          var expires = new Date(exp_y, exp_m, exp_d);
          cookie_string += "; expires=" + expires.toGMTString();
      }
  
      if (path)
          cookie_string += "; path=" + encodeURI(path);
  
      if (domain)
          cookie_string += "; domain=" + encodeURI(domain);
  
      if (secure)
          cookie_string += "; secure";
  
      document.cookie = cookie_string;
  }
  
  
  function delete_cookie(cookie_name)
  {
      var cookie_date = new Date();  // current date &amp; time
      cookie_date.setTime(cookie_date.getTime() - 1);
      document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
  }
  </code></pre>
  
  <p>but i am getting inconsistent results.
  for example, a cookie set on the startpage (www.example.com/start) , will not always show up on a subsequent page (www.example.com/foo/thing.jsp).
  i am setting a cookie "onUnload" of the page using </p>
  
  <pre><code>set_cookie("beginrequest", (new Date()).getTime(), null, null, null, "/");
  </code></pre>
  
  <p>and retrieving + deleting it "onLoad" via </p>
  
  <pre><code>loadDur = (new Date()).getTime() - get_cookie("beginrequest");
  delete_cookie("beginrequest");
  </code></pre>
  
  <p>to measure the total amount of time the page took to load.</p>
  
  <p>when using firebug, i often see "leftover" beginrequest-cookies and multiple instances of beginrequest with past timestamps.</p>
  
  <p>how can i achieve to see just one beginrequest-cookie on every page?</p>