We have a js function that runs on DOMContentLoaded, document.addEventListener(“DOMContentLoaded”, OURFUNCTION).
It works fine when you refresh the browser, but never shows when you navigate the site. I presume that’s because Discourse only loads the dom once and then the rest of handled with client side navigation, like an SPA. Some I’m wondering, how can we run the function on a navigation change in Discourse itself? There used to be easy ways to do that via the plugin API, but those APIs are deprecated and I don’t see them used anymore in any components. Is there an easy way to still do this? Or do we need to make a whole component to just execute some JavaScript upon a navigation change? THanks.
onPageChange is called the next runloop, so I think it will still be called after DOMContentLoaded in most situations, but I can’t say it’s guaranteed.
I can see usage relying on onPageChange and doing DOM element stuff directly. From where is fired the routeDidChange event btw (EDIT: it’s from ember: RouterService - 6.1 - Ember API Documentation)?
Thanks. I’ve read thru the documentation extensively and per my comments and a discourse member follow up there, the plugin API is legacy and will be deprecated soon. So while your code might work now, it will break soon, I believe in future updates. That’s why I’m looking for a better solution. Seems overkill to create a glimmer component for this so I was hoping there was another event that maybe we could use.
BTW, we tested it with api.OnPageChange and it works fine. We haven’t run into an instance yet here the DOM content wasn’t available upon calling OnPageChange, so it seems like it is fired after DomContentLoaded. But, I can’t be 100% sure. Thanks for your help on this.