Programming Tips - javaScript: detect when user uses back button to get to your page

Date: 2022nov23 Language: javaScript Q. javaScript: detect when user uses back button to get to your page A. There are many suggestions out there but this is the only one that worked for me in all browsers:
if (document.addEventListener) { window.addEventListener('pageshow', function(event) { const type = performance.getEntriesByType('navigation')[0].type; if (event.persisted || type === 'back_forward') { console.log('User arrived with <back so we are reloading'); location.reload(); } }, false); }
Credit: Tom Davenport on stackoverflow https://stackoverflow.com/questions/17432899/javascript-bfcache-pageshow-event-event-persisted-always-set-to-false