HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In addition to events that occur in response to user actions, several events depend on the operation of the browser itself.</p>
1 <p>In addition to events that occur in response to user actions, several events depend on the operation of the browser itself.</p>
2 <p>Such events include page loading and unloading events:</p>
2 <p>Such events include page loading and unloading events:</p>
3 <ul><li><em>DOMContentLoaded</em>- The DOM tree is fully built and ready to work</li>
3 <ul><li><em>DOMContentLoaded</em>- The DOM tree is fully built and ready to work</li>
4 <li><em>load</em>- all resources are loaded (pictures, styles, scripts, etc.)</li>
4 <li><em>load</em>- all resources are loaded (pictures, styles, scripts, etc.)</li>
5 <li><em>beforeunload</em>- occurs when the user tries to leave the page</li>
5 <li><em>beforeunload</em>- occurs when the user tries to leave the page</li>
6 </ul><p>The<em>DOMContentLoaded</em>event often has its place in DOM-dependent code. It helps to avoid errors related to the fact that scripts can start executing before the DOM they depend on is ready.</p>
6 </ul><p>The<em>DOMContentLoaded</em>event often has its place in DOM-dependent code. It helps to avoid errors related to the fact that scripts can start executing before the DOM they depend on is ready.</p>
7 <p>This event depends on the document:</p>
7 <p>This event depends on the document:</p>
8 <p>The speed of building the DOM tree depends on the &lt;script&gt; tags. By default, any &lt;script&gt; in HTML runs before the tree is complete.</p>
8 <p>The speed of building the DOM tree depends on the &lt;script&gt; tags. By default, any &lt;script&gt; in HTML runs before the tree is complete.</p>
9 <p>It means that the speed of code execution in this &lt;script&gt; block will influence when the user sees the site and when the<em>DOMContentLoaded</em>event is triggered.</p>
9 <p>It means that the speed of code execution in this &lt;script&gt; block will influence when the user sees the site and when the<em>DOMContentLoaded</em>event is triggered.</p>
10 <p>The topic of optimizing script loading and fast initialization is quite complicated. It is due not only to many factors affecting the order and speed but also to the fact that it works differently in different browsers:</p>
10 <p>The topic of optimizing script loading and fast initialization is quite complicated. It is due not only to many factors affecting the order and speed but also to the fact that it works differently in different browsers:</p>
11 <p>Pay attention to the red and blue stripes. The red one shows the triggering of the<em>load</em>event, and the blue one shows<em>DOMContentLoaded</em>.</p>
11 <p>Pay attention to the red and blue stripes. The red one shows the triggering of the<em>load</em>event, and the blue one shows<em>DOMContentLoaded</em>.</p>
12 <p>At the bottom of the picture, you can see the trigger time of each event, starting from when the page started loading. You can see that the browser first downloads the page itself (its HTML), then extracts links to all external resources from it and starts downloading them. For greater efficiency, downloading resources should happen simultaneously as often as possible.</p>
12 <p>At the bottom of the picture, you can see the trigger time of each event, starting from when the page started loading. You can see that the browser first downloads the page itself (its HTML), then extracts links to all external resources from it and starts downloading them. For greater efficiency, downloading resources should happen simultaneously as often as possible.</p>