Avatar ·

How to replace $(document).ready() in pure JavaScript (Vanilla JS) and execute code after DOM is loaded?

Hi everyone! I just started learning JavaScript, and before that I worked a bit with jQuery. Everything was simple there: to execute code after the page loads and the DOM tree is ready, I would write: ```javascript $(document).ready(function() { // my code }); ``` Or in short form: ```javascript $(function() { // my code }); ``` Now I decided to drop jQuery and write in pure JavaScript (Vanilla JS) to reduce page size and better understand how everything works under the hood. But I ran into a problem: how to properly make an analog of `$.ready()`? I tried using `window.onload`, but it only fires after all images and styles are fully loaded, and I need to execute code as soon as the DOM is built. For example, I want to attach handlers to