Setting cookie ignores expiration date

I need to set a cookie “fhr_showHideDivs” so the divs values in the cookievalue open (expand) when a visitor returns to the page. Here is my issue. The cookie gets set and the cookievalue gets written properly however the expiration date is ignored by chrome and chrome reports the expiration as a session, which removes the cookie when browser is closed, instead of a future date value, in this case one year. Can some explain what I am doing wrong?
the file name is: show_hide_divs.js and gets loaded in the header.

var fhr_showHideDivs = getCookie( "fhr_showHideDivs" );
var expireDate = new Date( );
expireDate.setTime( expireDate.getTime( ) + ( 1000 * 60 * 60 * 24 * 365 ));
var expires = "; expires= "+ expireDate.toUTCString();


function expandBlock( ID )
{
  document.getElementById( 'item' + ID ).style.display = 'block';
  document.getElementById( 'expander' + ID).innerHTML = '<a href="javascript:collapseBlock(' + ID + ');" title="Collapse this block">[-]</a>';
  fhr_showHideDivs = fhr_showHideDivs + ID + ",";
  setCookie( "fhr_showHideDivs", fhr_showHideDivs, expires );
}

function collapseBlock(ID)
{
  document.getElementById( 'item' + ID).style.display = 'none';
  document.getElementById( 'expander' + ID).innerHTML = '<a href="javascript:expandBlock(' + ID + ');" title="Expand this block">[+]</a>';
  var re = new RegExp( "," + ID + ",", "g" );
  fhr_showHideDivs = fhr_showHideDivs.toString( ).replace( re, "," );
  re = new RegExp( "^" + ID + ",", "g" );
  fhr_showHideDivs = fhr_showHideDivs.toString( ).replace( re, "" );
  setCookie( "fhr_showHideDivs", fhr_showHideDivs, expires );
}

function setCookie(cookieName, cookieValue, expires, path, domain, secure, same_site)
{
  document.cookie = escape( cookieName ) + '=' + escape( cookieValue ) + expires + ( path ? '; PATH=' + path : '' ) + ( domain ? '; DOMAIN=' + domain : '' ) + ( secure ? '; SECURE' : '' ) + ( same_site ? '; samesite=strict' : '' );
}

function getCookie(cookieName)
{
  var cookieValue = "";
  var posName = document.cookie.indexOf( escape( cookieName ) + '=' );
  if ( posName != -1 )
  {
    var posValue = posName + ( escape( cookieName ) + '=' ).length;
    var endPos = document.cookie.indexOf( ';', posValue );
    if ( endPos != -1 )
    {
      cookieValue = unescape( document.cookie.substring( posValue, endPos ) );
    }
    else
    {
      cookieValue = unescape( document.cookie.substring( posValue ) );
    }
  }
  return cookieValue;
}

function processDivs()
{
  if ( !fhr_showHideDivs.length )
  {
    return;
  }
  var blockNums = fhr_showHideDivs.split( "," );
  var i;
  for ( i=0; i < blockNums.length - 1; i++ )
  {
    if ( document.getElementById( 'item' + blockNums[i] ) != null )
    {
      document.getElementById( 'item' + blockNums[i] ).style.display = 'block';
      document.getElementById( 'expander' + blockNums[i] ).innerHTML = '<a href="javascript:collapseBlock(' + blockNums[i] + ');" title="Collapse this block">[-]</a>';
    }
  }
}

window.onload = processDivs;

Browsers are not designed to both set and retrieve the same cookie in one go.
This is because cookies are sent and updated with each page request.

It is expected that a cookie is set, then the page is refreshed or loaded again at some stage, before the cookie is used.

I appreciate the response, however that is not what I am doing. When the main page is loaded the script looks for the cookie, and it does not attempt to set the cookie until a DIV is expanded or collapsed on the main page, at that point the functions to expand or collapse are called and then the cookie is set from the function. The cookie that gets set does not have an expiration date and the browser treats it as a session only cookie. I am trying to figure out why the expiration date is not getting set to 1 year out.

Or are you saying when the call is made to expand or collapse a DIV, the vars that set the cookie date expiration are not read and only the function is processed. Do I need to move the set date var inside each function?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.