This function gets the current URL and Title, checks if the page is already in the breadcrumb, writes it to session storage and calls another function which builds the breadcrumb. The pageOrder Array guarantees the order of the pages visited, because sessionStorage is not ordered. The code works so far.
/**
* Adds the current page location to the breadcrumb
*/
const addLocation = () => {
const URL = window.location.href;
const title = document.title;
if (sessionStorage.getItem(title)) {
removeCircle(title);
}
sessionStorage.setItem(title, URL);
pageOrder.push(title);
buildBreadcrumb();
};
The following line adds an event listener for loading the page
// Event listeners
window.addEventListener('load', addLocation);
This event does not recognize changing pages on the same website, im looking for the correct event to call addLocation.
I tried the events 'popstate' and 'unbeforeunload', but popstate only is for clicking the forward or backwards button in the browser, and with unbeforeunload the created DOM-elements disappear after execution exits the buildBreadcrumb function