JavaScript Execution Time

Sometimes the JavaScript file is executed before the DOM is completely rendered. The problem caused by this is while trying to access the DOm elements, it gives an error saying the element does not exists. For the moment, We have use the setTimeOut method to delay the execution for half a second which is not a good solution nor a good practice.
So, the question is how to change the JavaScript file to execute only if the DOM is fully generated?

That’s expected if you build your extensions using inadequate hooks, like $( document ).ready or anything like that.

Discourse is an EmberJS app, so any extensions should take that into consideration and hook into the app existing hooks. You can learn about that at Beginner's guide to developing Discourse Themes


@Falco Thank you very much for responding. I will have a look at it and let you know if it worked