PHP
Joomla! 6
Joomla! 5
Joomla! 3 (Legacy)
Template
Basic
Load External JavaScript File with WebAssetManager
Registers and loads external or local JS scripts with defer via WebAssetManager to avoid render blocking.
templates/seu_template/index.php
<?php
declare(strict_types=1);
use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->registerAndUseScript(
'custom.external.js',
'https://cdn.example.com/scripts/app.min.js',
['version' => 'auto'],
['defer' => true],
['core']
);
Adds defer: true out of the box for non-blocking page rendering.
templates/seu_template/index.php
<?php
$document = JFactory::getDocument();
$document->addScript('https://cdn.example.com/scripts/app.min.js');
Legacy Joomla 3 version via JFactory::getDocument()->addScript().
Technical explanation and best practices
In Joomla 5 and 6, registerAndUseScript() supports dependency chains (such as core), non-blocking defer: true attributes for optimal Core Web Vitals, and automatic deduplication.