title: Using JavaScript Cookies as Global Variables
body: <p>I have a problem using JavaScript cookies as global variable. Basically I've set the cookie with the attribute prev to store a value on a jsp page.</p>
  
  <p>Then on another jsp page, I will try to obtain the value. However I do not get the value. I always got the wrong value.</p>
  
  <p>The two jsp page are abc.jsp and xyz.jsp. Both jsp pages have a &lt;%@ include file='cookies.jsp'%></p>
  
  <p>This is the content inside cookies.jsp. It is the same code as W3school example.</p>
  
  <pre><code>&lt;script&gt;
  /*Code For Setting Cookies*/
  function getCookie(c_name)
  {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i&lt;ARRcookies.length;i++)
    {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name)
      {
      return unescape(y);
      }
    }
  }
  
  function setCookie(c_name,value,exdays)
  {
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
  }
  
  function checkCookie()
  {
      var x=getCookie("prev");
      if (x!=null) //cookie is set
        {
          //do nothing
        }
      else 
        {
          setCookie("prev",0,365); //set to null
        }
  } //end of code for setting cookies
  &lt;/script&gt;
  </code></pre>
  
  <p>Most importantly, I tried to use getCookie() and setCookie(). It seems to set the cookies locally on the page but not transferring values across pages. May I know what happen?</p>
  
  <p>Please prompt me for more questions if you need more clue.</p>