There’s probably a few ways to address this, but let’s try the “proper” way using the right customization hooks.
When you add HTML to the footer section of a theme, Discourse does some handling for that so that it doesn’t jump around between page transitions or when more elements are added with infinite scroll.
It gets added as a custom-html
component here.
If you look at the component code, you’ll see that it fires an app event when it’s rendered.
This is the hook you need to use.
How do you use it?
There’s a method in the plugin-api that lets you fire code when a specific app event is fired.
Since we already know the event name, we can use that like so
api.onAppEvent("inserted-custom-html:footer", () => {
// do your work here
});
Since this is a plugin-API method, you need to change your script tag’s type to text/discourse-plugin
like so
<script type="text/discourse-plugin" version="0.8">
api.onAppEvent("inserted-custom-html:footer", () => {
// do your work here
});
</script>
The snippet above will fire whenever the footer is rendered. So, you can now do something like this.
<script type="text/discourse-plugin" version="0.8">
api.onAppEvent("inserted-custom-html:footer", () => {
$(".footer-div #footerNavDiv").click(function () {
console.log("click event captured");
// do your work here for the click event
});
});
</script>
You can read more about the plugin-API here