cssdomparserdomcontentloaded

Does <link> tag in <body> delays DomContentLoaded event?


I am trying to understand the impact of putting 'link' tag under 'head' v/s putting under 'body' on the DomContentLoaded event timings. I observed that when 'link' is under 'head' DomContentLoaded is fired immediately after DOM is ready but when 'link' is under 'body' the event is fired after the CSS file is downloaded and parsed. So, I want to know that why DOMContentLoaded is taking time when 'link' is under 'body'.

<!DOCTYPE html>
<html>

<head>
  <title>Rendering Understanding</title>
</head>

<body>
  <link rel='stylesheet' href='/stylesheets/style1.css' />
  <hr>
  <h2>
    Hello World!
  </h2>

</body>
</html>

Solution

  • I did some analysis and found the following observations in chrome(76.0.3809.100):-

    When is HTML parsing blocked?

    Note - Link tag does not blocks the HTML parser when placed inside 'head' tag but it blocks the HTML parsing when placed inside 'body' tag. Therefore DomContentLoaded event takes time to fire as the complete DOM is not ready by that time.