title: How to delete the javascript cookies when the session invalidates on its own?
body: <p>I have declared a cookie in my code which is created after specific function gets successful. I manipulate the cookie based on the method success or failure. Below is the code that I have used to create the cookie,</p>
  
  <pre><code>setCounterOfSuccess : function (c_name,value,days){
                  var expires='';
                  if (days){
                      var date=new Date();
                      date.setTime( date.getTime() + (days*24*60*60*1000) );
                      expires='; expires=' + date.toGMTString();
                  }
                  document.cookie= c_name + '=' + value + expires + '; path=/';
              },
  getCounterOfSuccess : function(c_name){
                  var nameEQ = c_name + '=';
                  var ca=document.cookie.split(';');
                  for (var i=0; i&lt;ca.length; i++) {
                      var c=ca[i];
                      while (c.charAt(0)==' ') c = c.substring(1,c.length);
                      if (c.indexOf(nameEQ)===0) 
                          return c.substring(nameEQ.length,c.length);
              }
                  return null;
              },
  
  deleteCounterOfSuccess : function (name){
                          this.setCounterOfSuccess (name,'',-1);
              },
  </code></pre>
  
  <p>I want these to be session cookies. So while setting the value I am setting them as below, I am passing ‘ ’ as last parameter to set them as session cookies.</p>
  
  <p>I assume they will expire when the browser will be closed.</p>
  
  <pre><code>this.setCounterOfSuccess ("counterOfSuccess", Counter, '');
  </code></pre>
  
  <p>I am also deleting the cookie when I hit the logout button from the website. It looks like below,</p>
  
  <pre><code>logout: function()
  {
  this.deleteCounterOfSuccess ("counterOfSuccess");
  }
  </code></pre>
  
  <p>So I am handling 2 ways of my how my cookies should get deleted. Now the problem is when I leave my website logged in for some time, it automatically logs out after a certain time period as expected. Which means that the session gets terminated or invalidated on its own. Now in this case if the browser is not closed and if I login again, the cookie is still there. And I don’t want that to happen. I want to have new cookie created at every session so that I can track the session related activities in the same.</p>
  
  <p>Can anyone please advise how can I achieve this? How can I delete this cookie when the session terminates on its own after leaving it alone for a long time?</p>
  
  <p>Thanks a lot.</p>