<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nasratullah Talash</title>
    <description>The latest articles on DEV Community by Nasratullah Talash (@nasratt).</description>
    <link>https://dev.to/nasratt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F460472%2F4910ba8c-261e-49f0-97ce-17da5e8a3999.png</url>
      <title>DEV Community: Nasratullah Talash</title>
      <link>https://dev.to/nasratt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nasratt"/>
    <language>en</language>
    <item>
      <title>Regular VS async VS defer script loading ⬇</title>
      <dc:creator>Nasratullah Talash</dc:creator>
      <pubDate>Fri, 11 Dec 2020 10:21:32 +0000</pubDate>
      <link>https://dev.to/nasratt/regular-vs-async-vs-defer-script-loading-1aa1</link>
      <guid>https://dev.to/nasratt/regular-vs-async-vs-defer-script-loading-1aa1</guid>
      <description>&lt;p&gt;We have always been advised to put out script tags at the end of the body tag, but have you ever thought why is that so? and is there any other place we can put our script tag?&lt;/p&gt;

&lt;p&gt;Well, yes, we can put the script tags anywhere we want but it affects the page performance. We can put the script tag as we normally do inside the &lt;code&gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;/code&gt; tag of the HTML document  like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- inside the head tag --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    ...
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"scriptURL/PATH"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or at the end of &lt;code&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- at the end of body tag --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    ...
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"scriptURL/PATH"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's try to explain the difference.&lt;/p&gt;

&lt;p&gt;When a page loads on the browser, the browser starts to parse the HTML, and when it reaches the script tag, it will load the script then execute it.&lt;/p&gt;

&lt;p&gt;When we put the script tag inside the &lt;code&gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;/code&gt; tag, the browser will reach to script tag before parsing the body. It stops parsing HTML for the sake of loading and executing the script. After the execution of the script is over, it will continue parsing HTML. Hence, our page loads slow and we may even get an error because DOM is not created completely yet. &lt;/p&gt;

&lt;p&gt;In the latter case, when we put the script tag at the end of the body tag, first the HTML is parsed and then the script gets fetched and executed. Huh, now we know why we put script tags at the end of the body tag (for performance and avoiding errors).&lt;/p&gt;

&lt;p&gt;HTML5 provides 2 new ways for loading scripts, that is async and defer attributes that can be added to the script tag. Adding these attributes when we have our script tag at the end of the body tag, doesn't make much sense (adding them is almost the same as not adding them). But, let's understand what they do when the script is inside the &lt;code&gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async attribute
&lt;/h2&gt;

&lt;p&gt;Adding the &lt;code&gt;async&lt;/code&gt; attribute to the script tag tells the browser to load or fetch script asynchronously while it is parsing the HTML. Whenever the script is loaded, it will get executed, so this way we reduce the time needed for the page to load.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defer attribute
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;defer&lt;/code&gt; attribute also tells the browser to load or fetch script asynchronously while it is parsing the HTML. Whenever the script is loaded, it will not get executed until HTML is not loaded and parsed completely.&lt;/p&gt;

&lt;p&gt;Wait, if &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; does almost the same job, why we have both of them? &lt;br&gt;
Well, it seems like that, but there are some differences between &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; doesn't guarantee that scripts will get executed in the order they are written in HTML (the sooner script is fetched, the sooner it is executed), and also &lt;code&gt;DOMContentLoaded&lt;/code&gt; event doesn't wait for the execution of the async scripts to be fired (&lt;code&gt;DOMContentLoaded&lt;/code&gt; event gets fired once the HTML is completely loaded and parsed).&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;defer&lt;/code&gt; guarantees that scripts will run in the order they appear in the HTML file and the &lt;code&gt;DOMContentLoaded&lt;/code&gt; event gets fired after all the scripts are loaded and executed.&lt;/p&gt;

&lt;p&gt;In conclusion, we can say that we should use &lt;code&gt;defer&lt;/code&gt; when the order of execution of scripts is important and/or the scripts rely on DOM being parsed fully. &lt;code&gt;async&lt;/code&gt; can be used for scripts, for which order of execution is not important and it does not rely on DOM.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>performance</category>
      <category>html</category>
    </item>
  </channel>
</rss>
