The discussion revolves around listening for events when the footer loads in Discourse, particularly to embed a Hubspot form that requires external JavaScript. e_division explains that the footer is reloaded on content refresh, making it difficult to target elements within it. They sought a way to run scripts when the footer is loaded, referencing outdated methods involving Discourse.View.reopen. eviltrout suggests a more advanced plugin approach, recommending the use of didInsertElement and willDestroyElement lifecycle hooks in components. travelvc shares a workaround using scroll event listeners and a flag to detect footer rendering, along with invalidating state on route changes. The consensus is that while no direct event listener exists, custom solutions involving component lifecycle methods or DOM detection can achieve the desired result.
Hi! I was looking through old forum posts looking for a way to listen for page loads, specifically the footer but found possible outdated or deprecated solutions. The problem I am facing is that there is no current way of embedding javascript into the footer and need to be able to target some items within the footer which is why I thought of trying to listen for some type of event that is fired after the footer is loaded.
Is the footer unloaded and then reloaded on every content refresh or is it always there after the initial load?
The solution I found involved tapping into the Discourse.View.reopen({…}) method. Is there something similar in Discourse in it’s current iteration?
@eviltrout
We have a Hubspot mailing list form in the footer that requires external javascript files hosted on Hubspot to work. There are two problems:
The footer component is removed and injected every time a user loads new content. The script would load on page load if placing it within the /body admin section but then lose it if new content is pushed and,
I can’t target anything within the footer component because it is injected last.
I tried placing the whole footer including the script within the /body admin section, which worked, but then I get the unwanted visual problem of the footer jumping to the top of the page every time new content comes in.
If I could run my scripts through a listener for when the footer is unloaded/loaded, I could probably keep the form in a temporary place while the footer reloads.
It’s always a little cumbersome to remove and add scripts repeatedly. Does their API allow you to say the page has refreshed? With sites like Discourse you’re really going to want to use a Javascript API that works on Ajax friendly sites like ours.
Also, “footer” is a bit misleading here as we have infinite scrolling. Users might never see it on long topics!
All we get from hubspot is an embeddable form. There are not many options available but the one that might help me is one option that allows me to target any div within the page to embed the form. The problem is I can’t target this div because the footer isn’t even there by the time the script is called. I’m guessing the answer is, no, there are no event listeners.
You can do anything in Discourse that you can in a regular page - it’s just a question of setting it up and tearing it down properly. It seems like a more advanced kind of plugin will be required for this, as it will involve creating a component and respecting didInsertElement/willDestroyElement.
If you’re looking for something similar our ad plugin works on a similar principle.