When Do Scripts and Stylesheets Delay DOMContentLoaded?
This article explains how script and stylesheet elements influence the timing of the DOMContentLoaded event, detailing the effects of async, defer, and external CSS on HTML parsing, supported by HTML5 specifications and performance analysis, to help developers optimize page load speed.
DOMContentLoaded Trigger Definition
After an HTML document is fully loaded and parsed, the DOMContentLoaded event fires. External style sheets and images usually do not affect this event, though special cases exist.
Script Tags
The parsing behavior changes based on script attributes:
async : Executes as soon as the script is available. It does not block parsing while the resource is being fetched, but if the script finishes loading before parsing completes, it will block parsing at that point. It only has an effect when the
srcattribute is present.
defer : Defers execution until after the document has been parsed, guaranteeing no parsing blockage.
Therefore, a script does not affect parsing when it has the
deferattribute, or when it has
asyncand the script finishes loading after parsing is already complete.
External Stylesheets
Stylesheets generally do not block HTML parsing because they do not modify the DOM tree. However, when a stylesheet is followed by a script, the script may be blocked until the stylesheet loads.
1. Stylesheet Followed by a Script
<code><html lang="en">
<head>
<link rel="stylesheet" href="//8.idqqimg.com/edu/assets/css/common_css_e582a1d4.css">
<script>console.log(121212)</script>
</head>
<body>
<p>23233</p>
</body>
</html>
</code>Performance analysis in Chrome shows that the script execution (yellow "Evaluate Script" bar) occurs after the stylesheet parsing, indicating a parsing delay.
2. Stylesheet Not Followed by a Script
<code><html lang="en">
<head>
<script>console.log(121212)</script>
<link rel="stylesheet" href="//8.idqqimg.com/edu/assets/css/common_css_e582a1d4.css">
</head>
<body>
<p>23233</p>
</body>
</html>
</code>Performance data shows the DCL line occurring almost simultaneously with the CSS request, confirming that the stylesheet does not block parsing in this case.
HTML5 Specification Insights
The parsing algorithm includes steps such as update document readiness (updating
readyStateand firing
readystatechange), then traversing the pending script list. Scripts only execute after confirming that no stylesheet is blocking scripts (
has no style sheet that is blocking scripts).
Only after these steps does the DOMContentLoaded event fire.
When a stylesheet blocks scripts, script execution is delayed, which in turn can delay parsing and the DOMContentLoaded event. User agents may choose to abort loading a stylesheet, but if a script queries style information before the stylesheet loads, it may receive default values, affecting page appearance.
Conclusion
Understanding how scripts and stylesheets affect document parsing helps developers optimize page load performance. Optimizations include removing render‑blocking JavaScript and improving CSS delivery.
Remove JS that blocks rendering
Optimize CSS delivery
Tencent IMWeb Frontend Team
IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.