<?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: Anselm Icheku</title>
    <description>The latest articles on DEV Community by Anselm Icheku (@anselm_icheku_5f40039adf6).</description>
    <link>https://dev.to/anselm_icheku_5f40039adf6</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%2F2038945%2Fa6bfb185-3c97-485e-b524-0f2d17fed893.png</url>
      <title>DEV Community: Anselm Icheku</title>
      <link>https://dev.to/anselm_icheku_5f40039adf6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anselm_icheku_5f40039adf6"/>
    <language>en</language>
    <item>
      <title>Optimising page Performance with proper &lt;script&gt; tag placement.</title>
      <dc:creator>Anselm Icheku</dc:creator>
      <pubDate>Sun, 08 Sep 2024 17:59:45 +0000</pubDate>
      <link>https://dev.to/anselm_icheku_5f40039adf6/optimising-page-performance-with-proper-tag-placement-2a62</link>
      <guid>https://dev.to/anselm_icheku_5f40039adf6/optimising-page-performance-with-proper-tag-placement-2a62</guid>
      <description>&lt;h2&gt;
  
  
  THE SCRIPT TAG
&lt;/h2&gt;

&lt;p&gt;The script tag is a quite interesting one. &lt;/p&gt;

&lt;p&gt;it plays a signicant role on how the html page functions because it contains the javascript or the typescript which controls how users interact with the page.&lt;/p&gt;


&lt;p&gt;There are quite some sections of html page where the  tag should be placed.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;The placement of the &amp;lt;script&amp;gt; tag in a HTML doc plays a significant role on how the script interacts with the page when it is executed.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;The position you place your &amp;lt;script&amp;gt; determines how fast your page loads and some other functionalities.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;Here is a breakdown of common positions to place your &amp;lt;code&amp;gt;&amp;amp;lt;script&amp;amp;gt;&amp;lt;/code&amp;gt;tags, the criteria and why it matters.&amp;lt;/p&amp;gt;&amp;lt;/p&amp;gt;&lt;br&gt;
&amp;lt;h2&amp;gt;&lt;br&gt;
  &amp;lt;a name="placing-ltscriptgt-in-the-ltheadgt-section" href="#placing-ltscriptgt-in-the-ltheadgt-section" class="anchor"&amp;gt;&lt;br&gt;
  &amp;lt;/a&amp;gt;&lt;br&gt;
  Placing &amp;lt;code&amp;gt;&amp;amp;lt;script&amp;amp;gt;&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;&amp;amp;lt;/head&amp;amp;gt;&amp;lt;/code&amp;gt; Section:&lt;br&gt;
&amp;lt;/h2&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;when &lt;code&gt;&amp;amp;lt;script&amp;amp;gt;&lt;/code&gt; is placed inside the &amp;lt;head&amp;gt;  the script is executed as soon as the browser reads it while parsing the HTML.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;This happens before any of the body elements (content) are rendered.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;When is it important to put the &lt;code&gt;&amp;amp;lt;script&amp;amp;gt;&lt;/code&gt; in the &lt;code&gt;&amp;amp;lt;head&amp;amp;gt;&lt;/code&gt; section?&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;If the script is non-interactive or doesn't need to manipulate DOM elements (e.g., analytics, metadata handling, styling-related JS), placing it in the &amp;lt;head&amp;gt; can be commendable.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;However, there are some downsides to consider when placing your tags in the &amp;lt;head&amp;gt; section.&amp;lt;/p&amp;gt;&lt;br&gt;
&amp;lt;div class="highlight"&amp;gt;&amp;lt;pre class="highlight plaintext"&amp;gt;&amp;lt;code&amp;gt;&amp;amp;lt;ul&amp;amp;gt;&lt;br&gt;
&amp;lt;/code&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;p&amp;gt;&amp;lt;li&amp;gt;• It can delay the rendering of the page, as the browser will stop loading other resources (HTML, CSS) until the script is fully executed.&amp;lt;br&amp;gt;&lt;br&gt;
   &amp;lt;li&amp;gt; •  If the script tries to access DOM elements that haven&amp;amp;#39;t been loaded yet, it will fail or cause errors.&amp;lt;br&amp;gt;&lt;br&gt;
&amp;lt;/ul&amp;gt;&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;&lt;br&gt;
Example:&amp;lt;br&amp;gt;&lt;br&gt;
&lt;code&gt;html  &amp;amp;lt;br&amp;amp;gt;&lt;br&gt;
  &amp;amp;lt;head&amp;amp;gt;  &amp;amp;lt;br&amp;amp;gt;&lt;br&gt;
&amp;amp;lt;title&amp;amp;gt;&amp;amp;lt;/title&amp;amp;gt;&amp;amp;lt;br&amp;amp;gt;&lt;br&gt;
   &amp;amp;lt;script src="script.js"&amp;amp;gt; &amp;lt;br&amp;gt;&lt;br&gt;
   &amp;lt;br&amp;gt;&lt;br&gt;
............&amp;lt;br&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt; &lt;br&gt;
&lt;h2&gt;
  
  
  Placing `&lt;code&gt;at the Bottom of&lt;/code&gt;&amp;lt;body&amp;gt;`
&lt;/h2&gt;

&lt;p&gt;&amp;lt;p&amp;gt;&lt;br&gt;
Another good position is in the &lt;code&gt;&amp;amp;lt;body&amp;amp;gt;&lt;/code&gt; section. &amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;Here the script is placed just before the closing body tag &amp;lt;/body&amp;gt;.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;In this position , the script is executed after the HTML content has been parsed and rendered by the browser.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;This is ideal when your JavaScript needs to manipulate DOM elements (e.g., adding event listeners, modifying content, animations).&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;This is important because by the time the script runs, all elements in the body are already loaded and accessible.&amp;lt;/p&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt;Ofcourse there are benefits to this method:&amp;lt;/p&amp;gt;  &lt;/p&gt;

&lt;p&gt;&amp;lt;p&amp;gt; - It allows the page content to load first, improving ,page load performance.&amp;lt;br&amp;gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoiding errors related to trying to access DOM elements before they exist.&amp;lt;br&amp;gt;
&amp;lt;/p&amp;gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;lt;p&amp;gt;Example:&amp;lt;br&amp;gt;&lt;br&gt;
  ```html  &amp;lt;br&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;body&amp;gt;    &amp;lt;br&amp;gt;&lt;br&gt;
    &amp;lt;!-- Page content --&amp;gt; &amp;lt;br&amp;gt;&lt;br&gt;
    &amp;lt;script src="script.js"&amp;gt; &lt;br&gt;&lt;br&gt;
  ; close to the &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
