const onLocationChange = (location: Location) => { if (location.pathname === "/" && location.hash === "") { document.documentElement.setAttribute("data-home", ""); } else { document.documentElement.removeAttribute("data-home"); } }; const getCurrentLocation = () => document.location; const currentLocation: Location = getCurrentLocation(); onLocationChange(currentLocation); window.addEventListener("popstate", (event) => { onLocationChange(getCurrentLocation()); }); // Listen for changes in the URL hash window.addEventListener("hashchange", () => { onLocationChange(getCurrentLocation()); });