Script Tags: Do They Belong in the Body or the Head Section?

A detailed guide to script tags and their ideal placement for optimal performance.

CSS and JavaScript are essential and widely-used programming languages in the field of web development. They are used to add styling and interactivity to web pages, allowing for the creation of interactive and dynamic content.

We’ve covered different ways to handle CSS in our recent blog.

While CSS is used for styles, JavaScript, which is a programming language, is used to add interactive and dynamic functionality to web pages. It can be used on both the front end and back end of web projects, allowing for the creation of full-stack web applications, using a single language.

In short, CSS and JavaScript are crucial for creating user-friendly and engaging web experiences. 

Modern apps are dynamic in nature and feature-rich thanks to the power of JavaScript as it enables developers to add features like form validation, user authentication, and logical operations, making it possible to create interactive and engaging web experiences. We can add JavaScript to our HTML files using the script tags.

Deep Dive into Script Tags

Script tags are used inside HTML files to incorporate JavaScript.

The script tag includes a variety of properties that may be used to control the script's behavior. We’ll cover the most important ones in this blog. 

Type & Source Attributes

One of the basic tags that you can add is the "type" attribute which specifies the media type of the script. The commonly used script type is JavaScript, as shown in the example below.

Another attribute is the "src" attribute which is used to specify the URL of an external script file.

If you are not using an external script and want to write JavaScript directly inside your HTML file, you do not need the ‘src’ attribute as you can see in the code snippet provided below.

Async and Defer Loading

In addition, the "async" and "defer" properties in script tags allow for more control over script execution, making the overall web development process more efficient and effective.

The main difference between the async and defer attributes lies in the script execution. If the async attribute is set, the script is executed as soon as it is available, without waiting for the rest of the page to load. This can be useful for scripts that are independent of the page's content and do not need to be executed in a specific order.

On the other hand, if the defer attribute is set, the script is executed after the page has finished parsing. This is useful for scripts that depend on the page's content, as it ensures that the script is executed after all the necessary content has been loaded.

In general, the async attribute is best used for scripts that do not need to be executed in a specific order, while the defer attribute is best used for scripts that depend on the page's content and need to be executed in a specific order.

With the help of these, developers can control the order in which scripts are executed, which is crucial for ensuring that their websites function properly. This allows developers to create robust and reliable websites and ensures that their users have a positive experience. Additionally, by being able to control the execution of scripts, developers can optimize the performance of their websites and make them more efficient.

How to Use JavaScript in HTML files?

We can use JavaScript using the <script /> tag and we can place it inside the head or body section.

You can directly write the JavaScript code inside the script tags in the head section.

The second way is that you can place the script tags inside the body section and write the JavaScript code.

Here is an example.

Although it is possible to write JavaScript code directly within an HTML file, it is not the most effective or efficient way to do so. This can make the HTML file large and difficult to read and can cause problems when running the file. 

A better approach is to include external JavaScript files in the HTML file using the <script> tag. This allows for cleaner and more organized code and makes it easier to manage and maintain the website. For that, you can place the <script> tag which calls the external file in the head or body section of the HTML file.

Let’s take an example.

In the head section:

In the body section:

Where should the script tags go for optimal performance?

There is no definitive answer to the question of where to place script tags as it depends on the purpose of the script. 

For scripts that are critical to the page contents and should be loaded first such as analytics, API, or authentication-type of scripts, it is recommended to place them in the head section of the HTML page. 

And other script tags should be placed at the bottom of the page before the closing </body> tag. These are scripts that you can afford to load after the web page is rendered like jQuery or animations script such as Lottie's animation scripts at the end.

Let’s take the example of Lottie scripts to create smooth animations. 

Here, we simply used a script tag to include Lottie animation and a lottie-player tag inside the body.

Let us first look at the output.

Animation GIF

Likewise, you may utilize numerous sorts of free Lottie animation on your website & this is one of the examples of how you can use the script tags to create a smooth experience for your users.

Some of the other popular use cases of script tags:

  1. Form Validation: Most websites nowadays have sign-up and login forms and that’s where script tags can be useful to validate user inputs like a correct email address or a strong password. 
  2. Chatbots: JavaScript can be useful to create chatbots with the help of Artificial Intelligence to provide quick customer support and improve the user experience. 
  3. Integration: With the help of scripts, you can integrate several other technologies like animations using Lottie script tags, TailwindCSS & jQuery.
  4. Interactivity: Without JavaScript, it is challenging to create animations and add any dynamic functionality to web pages. Tasks like loading custom user data from a server, digesting an API, or performing authentication cannot be done with just HTML and CSS.

Some more use cases of script tags are to authenticate users, create online surveys, and many more.

However, script tags can only make the UI interactive and dynamic; hence you always need a pixel-perfect UI in the first place to get started.

This is where can help you. It is a plugin for converting your design files into production ready front-end code. With the UI taken care of, you can now focus solely on interactivity and extending the code. The code generated by Locofy is production-ready and extensible and, moreover, you can add & write custom JS script for interactivity and further functionality.

If you want to learn more about how to convert your Figma or Adobe XD designs to React, React Native, HTML/CSS, Nextjs, and more, check out our docs.

Keep your loco mode on!
Stay updated on the latest Locofy announcements and product updates

© 2024, Locofy Pte Ltd. All Rights Reserved.