Basic JS Frontend - Embedded Script and On Page Change

I am just trying to fire some functions to update and load an ad via JS on page load, and the event that I found here on the forum doesnt seem to fire at all:

<script type="text/discourse-plugin" version="0.8.16">
    api.onPageChange(() => {
        updateTopAd();
    });
    
    function updateTopAd() {
        console.log('Page changed');
    }
</script>

Is there a different way of doing this from within a script tag?

AH, so sorry. I needed it in the HEAD, not the body. Thank you!

2 Likes