Running Javascript after either reload or page navigation

I’m trying to run a snippet of Javascript at the bottom of regular ‘single’ blog pages, but it is currently only running when I completely reload the page with the Refresh button. If I go directly to a post like this one it seems to always load, but if I start at the homepage (https://www.ryanwwest.com/) then click on a post, the Javascript will not run (evidenced by the Inspect console not showing the line This should always run, including when navigating between Hugo pages or reloading and no Giscus comment system at the bottom of the page).

My code is here and the partial is added here. The goal is to always run line 58 (onPageLoaded()) when opening any article, and you’ll see various methods I’ve tried at lines 84-104 such as document.addEventListener('DOMContentLoaded', ..., window.onload etc. Unfortunately, none of those worked consistently for me when navigating between Hugo pages.

It seems that navigating between pages in Hugo doesn’t cauase the DOMContentLoaded event to fire, so perhaps I just need to find an event that always fires even when navigating within the Hugo site? Or is there a better way to have Javascript always run specifically for Hugo pages? Thanks in advance.

Your theme using custom SPA-like navigation:

million:navigate is the event name.

2 Likes

That worked, thanks so much!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.