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;