Prevent JS errors when scrolling on pages that aren’t the main page

This commit is contained in:
Neil Brommer 2023-07-13 14:06:38 -07:00
parent e96efcd675
commit 7a9b57c56e

View file

@ -33,7 +33,10 @@ document.addEventListener("DOMContentLoaded", () => {
const observer = new IntersectionObserver(entries => { const observer = new IntersectionObserver(entries => {
entries.forEach(entry => { entries.forEach(entry => {
const id = entry.target.previousElementSibling.id; const id = entry.target.previousElementSibling.id;
const menuListItem = document.querySelector(`nav li a[href="/#${id}"]`).parentElement; const menuListItem = document.querySelector(`nav li a[href="/#${id}"]`)?.parentElement;
if (menuListItem == null)
return;
if (entry.intersectionRatio > 0) { if (entry.intersectionRatio > 0) {
menuListItem.classList.add("active"); menuListItem.classList.add("active");