1

I need to unload /media/vendor/jquery/js/jquery.min.js from Joomla 5 in my custom template. Anyone can help me?

1
  • What exactly do you mean by "unload"? If you could give us some context by way of background as to why you want to do this, then that would be helpful. Commented Mar 14, 2024 at 20:30

1 Answer 1

3

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'); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.