How to Add Custom JavaScript to Joomla: A Comprehensive Guide

When it comes to extending the functionality of your Joomla website, custom JavaScript (JS) can be a game-changer. Whether you want to add JavaScript to a Joomla article, include a JS file to your Joomla template, or insert JavaScript into a module, component, or plugin, this guide has got you covered.
Here, we’ll explain all you need to know about adding custom JavaScript to your Joomla site. So, without further ado, let’s dive in!

Adding JavaScript to a Joomla Article

When you want to inject JavaScript into a Joomla article, the process is relatively straightforward.

  1. Open the article you want to edit.
  2. In the editor, click on the “Toggle Editor” button. This will switch the editor to its raw HTML mode.
  3. In this mode, you can insert your custom JavaScript  directly into the article.
  4. If the code gets stripped then go to Global Configuration, switch the editor to no editor and add the code. Few editors may not allow you to add javascripts to the article.

Example to Add Javascript

 <script> let d = new Date(); document.body.innerHTML = "<h1>Today's date is " + d + "</h1>" </script>

Example to Add Javascript Files

<script src="file.js"></script>

It’s as simple as that! You can read more about creating and editing articles in Joomla here.

Adding a JS File to a Joomla Template

If you want to add a JavaScript file to a Joomla template, you need to edit the template’s index.php file.
Here’s how:

  1. Go to the Joomla 4 administrator panel.
  2. Click on System > Site Templates
  3. Select your template and click on “Edit Main Page template.”
  4. Find a suitable location in the index.php file to insert your JavaScript file. It’s best to place it before the </head> tag.
  5. Include the JavaScript file by adding <script src="path_to_your_file.js"></script>. Replace “path_to_your_file.js” with the actual path to your JavaScript file.
  6. You can do the same using FTP

You can also add the javascript file using the following PHP code

<?php
$document = JFactory::getDocument();
$document->addScript('/media/system/js/sample.js');
?>

The above code instructs Joomla to load the sample.js file from the specified path. Here, the path is relative to your Joomla root directory. To ensure the right path, use the absolute URL to JavaScript in Joomla.
When you add a JS file to a Joomla template, you allow for cleaner, more manageable code, and ensure that the same script can be used across different parts of your website.
For more insights on working with Joomla templates, check out this detailed guide.

Adding JavaScript to Joomla Modules, Components, or Plugins

Just as with  templates, you can add custom JavaScript to Joomla modules, components, or plugins. This could be particularly useful if you want a specific module, component, or plugin to carry out a JavaScript function.
To add JavaScript to a Joomla module, you would typically edit the module’s PHP files like default.php which can be generally found inside the tmpl folder and insert your JavaScript code. However, always ensure that you follow the Joomla best practices to prevent any potential conflicts or errors.

Frequently Asked Questions​

How do I add JavaScript to a Joomla article?

You can add JavaScript to a Joomla article by opening the article for editing, switching the editor to raw HTML mode, and inserting your custom JavaScript between <script> tags.

How do I add JavaScript to a Joomla module?

You can add JavaScript to a Joomla module by locating the module’s PHP file which can be found at

modules/mod_your_module/tmpl/default.php. Edit the default.php file and insert the code with addScript.

How do I add an absolute URL to JavaScript in Joomla?

You can add an absolute URL to JavaScript in Joomla by adding the full URL in the src attribute of the script tag, like so: <script src="https://example.com/path_to_your_file.js"></script>.

How do I add JavaScript to Joomla’s head?

You can add JavaScript to Joomla’s head by editing your template’s index.php file and inserting your script tag before the </head> tag.

Alternately when you add the addScript Joomla function, it will automatically add all the scripts into the head of the Joomla website.

For more tips, tricks, and guides on how to make the most of your Joomla site, be sure to check out our blog.

Finally, if you’re looking to enhance your Joomla site even more, consider hiring a Joomla programmer. A professional can help you incorporate more advanced features and ensure your website is running smoothly.

If you have any questions about adding custom JavaScript to Joomla or any other Joomla-related topic, feel free to reach out. We’re here to help!

This article is intended for educational purposes only. All code should be used responsibly and appropriately. Always remember to backup your site and test in a staging environment before making changes to your live site.

For more information on Joomla, check out the official Joomla documentation.

Share the Article

Picture of Abhilash Sahoo

Abhilash Sahoo

Abhilash Sahoo, with 14 years of experience, is a Certified Joomla and WordPress Expert and the Founder & CEO of Infyways Solutions, specializing in innovative web development solutions.