I need to unload /media/vendor/jquery/js/jquery.min.js from Joomla 5 in my custom template. Anyone can help me?
1 Answer
You shouldn't. If jQuery is loaded, it's because something is using it. Disabling it may break some functionality. That said, the API for disabling assets is Joomla\CMS\WebAsset\WebAssetManager::disableAsset() method:
$webAssetManager = $this->getWebAssetManager(); if ($webAssetManager->getRegistry()->exists('script', 'jquery')) { $webAssetManager->disableAsset('script', 'jquery'); } But it may not work for a couple of reasons. First, if something re-enables the script after the template has been parsed, e.g. a plugin or a module. This issue can be avoided by running the code during some later plugin event:
$app->getDispatcher()->addListener( 'onBeforeCompileHead', static function (Joomla\CMS\Event\Application\BeforeCompileHeadEvent $event) { $webAssetManager = $event->getDocument()->getWebAssetManager(); if ($webAssetManager->getRegistry()->exists('script', 'jquery')) { $webAssetManager->disableAsset('script', 'jquery'); } } ); But then there's another issue. This method can't be used to disable dependencies. So if jQuery is a dependency of another asset, this will not work at all. There's a dirty workaround to this by overriding the asset using an empty URI. This can be done by adding the asset to your joomla.asset.json:
"assets": [ { "name": "jquery", "type": "script", "uri": "" } ] Or it can be done programmatically:
$this->getWebAssetManager()->registerAsset('script', 'jquery');