

Outside of checking the final dimensions of a loaded image or creating aĬrude progress bar to indicate progress. My years of doing things in JavaScript, I never had too much use
Run function as soon as element exists code#
Var images = document.querySelectorAll("img") įor cases where you want your code to run only after your page Overheard: document.addEventListener("DOMContentLoaded", theDomHasLoaded, false) The code that relies on the DOM to run only after that event is If you try toĪccess your DOM before it has fully loaded, you may get incompleteĪ sure-fire way to ensure you never get into a situation where yourĬode runs before your DOM is ready is to listen for the DOMContentLoaded event and let all of That relies on working with the DOM such as anything that uses theĮnsure your code runs only after your DOM has been fully loaded. Now that we got the boring technical details out of the way, why are these events important? Simple. Need to listen to DOMContentLoaded from the You use these events just like you would any otherĮvent, but the main thing to note about these events is that you Window.addEventListener("load", pageFullyLoaded, false)

You can use these events to timeīelow is a snippet of these events in action: document.addEventListener("DOMContentLoaded", theDomHasLoaded, false)

The DOMContentLoaded event firesĪt the end of Stage #2 when your page's DOM is fully parsed. There are two events that represent the two importants milestones Refer back to these stages a few times in the following sections. Hat if you are wearing one while reading this), for we'll Keep these three stages at the tip of your fingers (or under your Through when loading content, let's move forward to the more interesting Now that you have a basic idea of the three stages your document goes This is the stage where your browser's loading indicators stopĪnimating, and this is also the stage you almost always find yourself in when Stylesheets, scripts, and other external resources making their way into what you The final stage is where your page is fully loaded with any images, You only see the rawĬontent specified by your page/document's markup. Images and stylesheets have not been parsed. The thing to note about this stage is that external resources like Get a bit more exciting with the second stage where the raw markup and Has been made to load a page, but nothing has been downloaded yet. The first stage is when your browser is about toĪt this stage, there isn't anything interesting going on. Before we get there, though, let's take a quick detour and Re-learn) what all of these things do and how they impact when your code The location your scripts live in the DOMĭon't worry if you don't know what these things are.The defer attribute for script elements.The async attribute for script elements.Some point while your page is getting loaded: When exactly the code runsĭepends on a combination of the following things that all come alive at That happens is that any code specified on the page will run. One example of a relevant and interesting stuff Your page loading, many relevant and interesting stuff happen that you In that short period of time between you wanting to load a page and Of time to complete from beginning to end:

All of that seems pretty simple and takes up a very tiny sliver You click on a link or press EnterĪfter typing in a URL and, if the stars are aligned properly, your page BUY ON AMAZON The Things That Happen During Page Load To kick your JavaScript skills into outer space, everything you need to become a JS expert is available in both paperback and digital editions.
