Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for CSS, JavaScript, and blocking web page parsing
Matvey Romanov
Matvey Romanov

Posted on

     

CSS, JavaScript, and blocking web page parsing

Recently, I came across an article about the problem of loading CSS files, which slows down the processing of page materials. I read that article, trying to learn something new, but it seemed to me that what it said was not quite true. So I did my own research on this topic and experimented with loading CSS and JavaScript.

Can loading CSS resources block page parsing?

First of all, I will say that the question in the title of this section can, without any doubt, be answered positively. Loading CSS files can not only block HTML code parsing, but also prevent JavaScript code from executing.

To begin with, I suggest experimenting. To do this, we will need to configure the browser accordingly. We will download the CSS file from the CDN, so we will limit the speed of working with the network in the Google Chrome browser. To do this , on the developer tools tabPerformance, change the parameter value Networkto Slow 3G. We will explore the next page:

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metadata-fr-http-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><linkhref="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css"rel="stylesheet"><script>document.addEventListener('DOMContentLoaded',()=>{console.log('DOMContentLoaded');})</script><script>console.log('script');Promise.resolve(1).then(res=>{console.log('then');});</script></head><body><h1>hello</h1></body></html>
Enter fullscreen modeExit fullscreen mode

We download the CSS file from the CDN, but since the Internet connection speed is artificially limited, it will take some time to load the styles. As a result, nothing gets to the JavaScript console before the CSS file is loaded, and the page content is not displayed on the screen. What we're seeing indicates that CSS loading is blocking other page content from being loaded and processed.

imageOutput of data to the JS console

Can loading and executing JS code block page parsing?

Loading and processing JS files, of course, blocks page parsing. However, to fix this problem, you can use attributes and tags when connecting scripts to the pagedefer async <script>. Now we will study their impact on page loading.

Normal script loading and execution

If the tag<script> does not useasync or attributesdefer — the page content loading and processing process is performed as shown in the following diagram. Loading JS files and executing the code contained in them blocks HTML parsing.

imageUsing the<script> tag without the async and defer attributes

Here and further, we will use the following color symbols.

imageHTML parsing — HTML parsing; HTML parsing paused-HTML parsing suspended; Script download — Script loading; Script execution — Script execution

Using the<script> tag with the async attribute

When the browser processes a tag<script> with an attributeasync, the JavaScript code is loaded asynchronously. The script code is executed immediately after loading. However, JS code execution blocks HTML parsing.

imageUsing the<script> tag with the async attribute

Using the<script> tag with the defer attribute

If the tag<script> contains an attributedefer — the script code is loaded asynchronously. However, after the code is loaded, it is executed only when the parsing of the HTML code is completed.

imageUsing the<script> tag with the defer attribute

Experiments

Let's experiment withasync the and attributesdefer. Let's start with the next page:

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metadata-fr-http-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>DomContentLoaded</title></head><body><scriptsrc="http://code.jquery.com/jquery-1.4.4.min.js"></script><scriptsrc="./index.js"/>// 0<scriptsrc="./index2.js"/>// 2<script>console.log('inline');Promise.resolve().then(res=>{console.log('then');})</script><divid="hello">hello world</div><script>document.addEventListener('DOMContentLoaded',()=>{console.log('DOMContentLoaded');})</script></body></html>
Enter fullscreen modeExit fullscreen mode

This page, in addition to downloading the scriptjquery-1.4.4.min.js from the CDN, loads a couple of its own scripts -index.js andindex2.js. Below is their code.

Fileindex.js:

Promise.resolve().then((res)=>{console.log('index1');returnres;});
Enter fullscreen modeExit fullscreen mode

Fileindex2.js:

Promise.resolve().then((res)=>{console.log('index2');returnres;});
Enter fullscreen modeExit fullscreen mode

When this page loads, the JS console gets what is shown below.

imageOutput of data to the JS console

As a result, we have proof that loading and processing JS files blocks HTML code rendering. Messages output by scripts appear in the console before the message indicating that the DOM content has finished loading.

Now let's take a look at how scripts<script> that use the attribute in their tags behave<async>:

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metadata-fr-http-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>DomContentLoaded</title></head><body><scriptasyncsrc="http://code.jquery.com/jquery-1.4.4.min.js"></script><scriptsrc="./index.js"></script><scriptsrc="./index2.js"/></script><script>console.log('inline');Promise.resolve().then(res=>{console.log('then');})</script><divid="hello">hello world</div><script>document.addEventListener('DOMContentLoaded',()=>{console.log('DOMContentLoaded');})</script></body></html>
Enter fullscreen modeExit fullscreen mode

Let's look at what is shown in the console.

imageOutput of data to the JS console

The jQuery library script is loaded asynchronously. What goes to the console is displayed there before it is loaded. If the library script loads too slowly, it won't interfere with parsing the HTML code. The messageDOMContentLoaded can be displayed either before or after the async script is loaded and executed. And when the attributedefer is applied, the script will be loaded asynchronously, wait for the document materials to be processed, and then , but before the eventDOMContentLoaded, it will be executed.

Have you ever encountered problems with blocking the processing of web page content?

Top comments(7)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
sandhilt profile image
Bruno Ochotorena
  • Location
    Rio de Janeiro, Brazil
  • Work
    Junior Fullstack
  • Joined

I think that async allow code execute without stop parse of html.
Read here:
"If async is present: The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing)"
source:w3schools.com/tags/att_script_asyn...

CollapseExpand
 
rahulmanek profile image
rahulmanek
  • Work
    Full Stack Developer
  • Joined

Yes i also definitely agree with this.
async executes while HTML loading.
Checked withidevelop.pro/ consoles.

CollapseExpand
 
faheem_khan_dev profile image
Faheem Khan
JavaScript Developer | Works on React and Node | Blogger | Content Creator
  • Location
    India
  • Education
    B.Tech (2020)
  • Work
    Software Developer
  • Joined

This is not correct.
If async ia there, script "download" happens asynchronously not the execution.

CollapseExpand
 
sandhilt profile image
Bruno Ochotorena
  • Location
    Rio de Janeiro, Brazil
  • Work
    Junior Fullstack
  • Joined
CollapseExpand
 
sandhilt profile image
Bruno Ochotorena
  • Location
    Rio de Janeiro, Brazil
  • Work
    Junior Fullstack
  • Joined

Look in source:
"If the async attribute is set, the script is downloaded in parallel to parsing the page, and executed as soon as it is available."

CollapseExpand
 
quantuumsnot profile image
quantuumsnot
Coding as a hobby
  • Location
    Bulgaria
  • Work
    Sales
  • Joined

In simple words we mustdefer the script(s) instead ofasync if we want them to be executed properly on already LOADED HTML content

CollapseExpand
 
sandhilt profile image
Bruno Ochotorena
  • Location
    Rio de Janeiro, Brazil
  • Work
    Junior Fullstack
  • Joined

You can use async + DOMContentLoaded event.
TL;DR; DOMContentLoaded is event when all html is parsed and is safety manipulated.

So, script is async downloaded and execute waiting DOM is ready

Example:

<!-- index.html --><!DOCTYPE html><html><head><metacharset="UTF-8"><title>Example</title><scriptasyncsrc="./test.js"/></head><body><main><sectionid="example"></section></main>// ...</body></html>
Enter fullscreen modeExit fullscreen mode
/* test.js */(()=>{functionrun(){constexample=document.getElementById("example");console.log({example})// ...}window.addEventListener('DOMContentLoaded',run);})();
Enter fullscreen modeExit fullscreen mode

More info about event here:developer.mozilla.org/en-US/docs/W...

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hello, world! I'm a professional self taught full-stack developer from Moscow. I truly love web-development and all that it concerns. Making websites is awesome. Follow me if you need some help
  • Location
    Moscow, Russia
  • Work
    Fullstack developer, Team lead, DEV Mod
  • Joined

More fromMatvey Romanov

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp