DEV Community

Cover image for Optimising page Performance with proper <script> tag placement.
Anselm Icheku
Anselm Icheku

Posted on • Edited on

Optimising page Performance with proper <script> tag placement.

THE SCRIPT TAG

The script tag is a quite interesting one.

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.

There are quite some sections of html page where the tag should be placed.</p>

<p>The placement of the <script> tag in a HTML doc plays a significant role on how the script interacts with the page when it is executed.</p>

<p>The position you place your <script> determines how fast your page loads and some other functionalities.</p>

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

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

<p>This happens before any of the body elements (content) are rendered.</p>

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

<p>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 <head> can be commendable.</p>

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

<p>
Example:<br>
html &lt;br&gt;
&lt;head&gt; &lt;br&gt;
&lt;title&gt;&lt;/title&gt;&lt;br&gt;
&lt;script src="script.js"&gt; <br>
<br>
............<br>


Placing `at the Bottom of<body>`

<p>
Another good position is in the &lt;body&gt; section. </p>

<p>Here the script is placed just before the closing body tag </body>.</p>

<p>In this position , the script is executed after the HTML content has been parsed and rendered by the browser.</p>

<p>This is ideal when your JavaScript needs to manipulate DOM elements (e.g., adding event listeners, modifying content, animations).</p>

<p>This is important because by the time the script runs, all elements in the body are already loaded and accessible.</p>

<p>Ofcourse there are benefits to this method:</p>

<p> - It allows the page content to load first, improving ,page load performance.<br>

  • Avoiding errors related to trying to access DOM elements before they exist.<br> </p>

<p>Example:<br>
```html <br>

<body> <br>
<!-- Page content --> <br>
<script src="script.js">

; close to the

Top comments (0)