I am having problems with the accordion menu on my website. The menu works fine, but the latest version of Google Lighthouse is now telling me that the Javascript is flawed. Lighthouse is now complaining that the script "Registers an unload listener". The report goes on to say: "The `unload` event does not fire reliably and listening for it can prevent browser optimizations like the Back-Forward Cache. Consider using the `pagehide` or `visibilitychange` events instead."

Can someone suggest a way to fix this problem?

Many thanks, Ellen

The page is here: https://wildadirondacks.org/adironda...-striatus.html

The Javascript is here:


Code:
(function(){
	var accs = document.querySelectorAll('.accordion, .accordionSecond'),
	cookiename = 'accordionpersiting_73561',
	persisted = document.cookie.match('(^|;)\x20*' + cookiename + '=([^;]*)'),
	persisting = {}, i = accs.length, toggleClass = accs[0] && accs[0].classList? 
		function(el, name){el.classList.toggle(name);} : function(el, name){
			var classes = el.className.split(' '), i = classes.length, has = false;
			while(--i > -1){if(classes[i] === name){has = true; classes.splice(i, 1); break;}}
			!has && classes.push(name);
			el.className = classes.join(' ');	
		};
	if(persisted && (persisted = persisted[2])){
		var p = persisted.split('.'), l = p.length;
		while(--l > -1){persisting[p[l]] = true;}
	}
	function clickfunction(trans){
		var panel = this.nextElementSibling;
		toggleClass(this, 'active');
		if(trans === false){toggleClass(panel, 'notransition');}
		panel.style.maxHeight = panel.style.maxHeight? null : panel.scrollHeight + "px";
		if(trans === false){panel.offsetHeight; toggleClass(panel, 'notransition');}
		persisting[this.getAttribute('data-index')] = !!panel.style.maxHeight;
	}
	while(--i > -1){
		accs[i].setAttribute('data-index', i);
		accs[i].addEventListener('click', clickfunction, false);
		persisting[i] && clickfunction.apply(accs[i], [false]);
	}
	window.addEventListener('unload', function(){
		var pa = [], p;
		for(p in persisting){persisting.hasOwnProperty(p) && persisting[p] && pa.push(p);}
		document.cookie = cookiename + "=" + pa.join('.') + "; path=/";
	}, false);
})();