(Squarespace) Load the new page content immediately after users click the navigation item

You can use this code to Code Injection > Footer.
If code doesn’t work, you can comment below, message or email me.

<script>
window.addEventListener('DOMContentLoaded', () => {
  const navItems = document.querySelectorAll('.header-nav-item a');
  
  navItems.forEach(item => {
    item.addEventListener('click', (e) => {
      e.preventDefault();
      const href = item.getAttribute('href');
      
      if (href && !href.startsWith('#') && !href.includes('http')) {
        fetch(href)
          .then(response => response.text())
          .then(html => {
            const parser = new DOMParser();
            const doc = parser.parseFromString(html, 'text/html');
            const pageContent = doc.querySelector('#page');
            
            if (pageContent) {
              document.querySelector('#page').innerHTML = pageContent.innerHTML;
              window.history.pushState({}, '', href);
            }
          });
      }
    });
  });
});

window.addEventListener('popstate', () => {
  fetch(window.location.pathname)
    .then(response => response.text())
    .then(html => {
      const parser = new DOMParser();
      const doc = parser.parseFromString(html, 'text/html');
      const pageContent = doc.querySelector('#page');
      document.querySelector('#page').innerHTML = pageContent.innerHTML;
    });
});
</script>

1 Like