I am building a dynamic dashboard and I need to trigger an initialization function—like fetching data or setting up event listeners—as soon as the page is ready. I’ve tried using the onload attribute in the <body> tag, but it feels outdated and clutters my HTML. Is there a more modern way to do this using pure JavaScript (Vanilla JS) or a specific React hook? I want to ensure the DOM is fully constructed before the function runs to avoid "null" reference errors when accessing elements.
3 answers
Don't forget that you can also simply place your <script> tag at the very bottom of the <body>. In modern browsers, using the defer attribute in your script tag in the <head> achieves the same non-blocking result without needing an explicit event listener!
Still have questions? Schedule a free counselling session
Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.
Stay Updated
Subscribe to our newsletter for the latest updates on certifications and training opportunities.
Search Online
Search iCertGlobal for certifications, courses and articles.
Accredited by industry leaders
We Accept
Rated by learners
Review us on TrustpilotShare your experience"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

I agree with Jennifer. Using