Here I add eventlistner, where i add click event for the button. When i click the button it will redirect to another page.
In this case do i need to remove eventlistner manually from the DOM.
or as we are redircting to the another page it will take care by the platform itself.
EventListner - will keep the event running even when go to another page.
What is the best approach, should i remove it from the DOM manually on OnDestroy?
yes, remove eventListeners in the OnDestroy
but, more importantly, don't do in Javascript what you can do in the OutSystems code. If you want an action to run when a button is clicked, you can already do that in OutSystems, no need for Javascript.
Dorine
General Syntax of addEventListener
document.addEventListener(type, listener [, options]);
If you set the option { once: true }, the listener is automatically removed after being triggered once. For example:
btn.addEventListener("click", handleClick, { once: true });
However, if the "click" event never occurs, the event listener will not be removed, potentially leading to memory issues. To avoid this, you should explicitly remove the event listener in OnDestroy.
Suggested Implementation
Define a Local Variable ( of type Object) to Store the Event Handler: Assign a JavaScript function to $parameters.Handler in OnReady:
$parameters.Handler = function handleClick() { console.log("Button clicked!"); $actions.VirtualBtnClick(); };
Manage Event Listeners in EventListenerOnClick:
Clean Up in OnDestroy: Explicitly remove the event listener to ensure there are no memory leaks:
var btn = document.getElementById($parameters.ButtonId); btn.removeEventListener("click", $parameters.Handler);
Note : To remove a listener, JavaScript requires the exact same function reference used in addEventListener. If the reference is different, removeEventListener will silently fail and the listener won't be removed. Hence the function reference is kept in the $parameters.Handler and used in add and remove event handlers.
Hi pradip,
For in this case, you don't need to do anything in OnDestroy. If there are any active processes or tasks, you might need to clean up the dom. However, for simple navigation actions, it's completely fine to leave onDestroy empty