Trigger javascript on clicking any page link, but before the page content loads?

Happy to be explicit as the code is open source:

I’m working with a third party ad provider and they have an ad page reset function in javascript that needs to be run before the new page’s ads are triggered.

any improvement is welcome and I’ve already realised this on the Topic List with Johani’s code. (in an open PR).

But this left the Topic … I will try your suggestion next and adopt it in both instances if that improves things further.