-
-
- Submit Updated Articles to Bing and Yandex with IndexNow in Joomla
- Add Published and Modified Date Meta Tags to Joomla
- Add Open Graph and Twitter Card Tags to Joomla
- Auto-Generate Meta Descriptions from Article Text in Joomla
- Add Noindex to Specific Pages in Joomla
- Verify Your Joomla Site with Google Search Console
-
- Add a View as Markdown Link to Joomla Articles
- Display the Last Modified Date in Joomla Articles
- Embed Iframes in Joomla
- Automatically Link Twitter (X) @mentions in Joomla Articles
- Open External Links in a New Tab in Joomla
- Display Estimated Reading Time in Joomla Articles
- Replace Old URLs in Joomla
- Search and Replace in Joomla
-
- Add a Footer Line to the Joomla Administrator
- Show a Staging Banner in the Joomla Administrator
- Remove the "All" Option from Admin List Limits in Joomla
- Add a Cookie Consent Banner to Joomla
- Make the Administrator Navigation Sticky in Joomla
- Add Google Fonts to Joomla
- Add a Facebook Messenger Chat Button to Joomla
- Add a Reading Progress Bar to Joomla
- Add a Custom Scrollbar to Joomla
- Show Confetti on a Thank You Page in Joomla
- Add Smooth Scrolling to Joomla
- Add a Back to Top Button in Joomla
- Add a WhatsApp Chat Button in Joomla
-
- Protect the Administrator with a Secret URL Key in Joomla
- Show a Welcome Message on First Login in Joomla
- Log Out Idle Administrators in Joomla
- Assign New Users to a Different User Group Automatically in Joomla
- Redirect Visitors by Country in Joomla
- Redirect Users After Login by User Group in Joomla
- Block User Registration with Certain Usernames in Joomla
- Block User Registration by Country in Joomla
- Block User Registration by Email Address in Joomla
- Block User Registration by IP Address in Joomla
- Remove the Forgot Password and Username Links in Joomla
- Remove the Generator Meta Tag in Joomla
- Let Users Log In with Their Email Address in Joomla
- Block Traffic by Country in Joomla
- Block Bot Traffic in Joomla
- Block Traffic by IP Address in Joomla
Defer JavaScript in Joomla
Speed up your Joomla pages and clear the render-blocking warning in PageSpeed Insights by deferring JavaScript, using one PHP snippet.
PageSpeed Insights keeps telling you to eliminate render-blocking resources, and most of the offenders are JavaScript files that your template and extensions load in the head. Each one delays first paint while the browser stops parsing HTML to fetch and run it. Deferring JavaScript in Joomla fixes that without touching a single template file, and it doesn't require a full optimization extension. Hopefully, you don't need an extra plugin. You can do it with Tassos Code Snippets using a simple PHP snippet.
The PHP Snippet Code
Set Code Type to PHP, then use this snippet. It runs right before Joomla compiles the document head, walks through every script registered with the Web Asset Manager, and adds the defer attribute to it. Deferred scripts still download in parallel, but they execute after the HTML is parsed, so they stop blocking the render. Anything you list in $exclude is skipped, which is where you put scripts that must run immediately.
use Joomla\CMS\Factory;
use Joomla\CMS\Document\HtmlDocument;
// Scripts whose name contains any of these fragments will NOT be deferred.
// Matching is case-insensitive and matches anywhere in the asset name.
$exclude = [
'core',
'keepalive',
];
// Do not edit below.
$app = Factory::getApplication();
$app->registerEvent('onBeforeCompileHead', function () use ($app, $exclude)
{
$document = $app->getDocument();
if (!($document instanceof HtmlDocument))
{
return;
}
$needles = array_filter(array_map('strtolower', array_map('trim', $exclude)));
$wa = $document->getWebAssetManager();
foreach ($wa->getAssets('script', true) as $asset)
{
$name = strtolower($asset->getName());
foreach ($needles as $needle)
{
if (strpos($name, $needle) !== false)
{
continue 2;
}
}
// Module scripts are deferred by the browser already.
if ($asset->getAttribute('type') === 'module')
{
continue;
}
$asset->setAttribute('defer', true);
}
});
Tip: Deferring changes execution order, so test your site after publishing. If a slider, gallery, or form stops working, find the script's asset name and add a distinctive fragment of it to $exclude rather than removing the snippet. Inline scripts are not touched by this snippet, only registered script files.
How to add this snippet
- Install Tassos Code Snippets if it is not installed already.
- Go to your Joomla Administrator area.
- Open Components → Tassos Code Snippets.
- Click New.
- Select PHP as the snippet type.
- Paste the code to defer JavaScript above.
- Select Insertion Method → Page Load.
- Publish the snippet.
- Optionally, open the Conditional Logic tab to restrict which pages or users the snippet runs on. Leave it empty to run it site-wide. Learn more in Using Conditional Logic.
Congrats! You've just added JavaScript deferring functionality to Joomla without installing another plugin or adding unnecessary bloat to your site.
For instance, to learn more about how the PHP Snippet works, visit our documentation: https://www.tassos.gr/docs/tassos-code-snippets/types/php