That isn’t how you’d do it there. To do it there, you’d have to do something like
<script type="text/discourse-plugin" version="0.4">
api.onPageChange((url, title) => {
alert('test');
});
</script>
My original post was from a plugin perspective.