Tell me please. How to load jQuery in Joomla 4 if it has not been loaded yet?
-
Your question is clear, but has been flagged by the system as being low-quality due to its length. Might you edit your question to include attempts that you've made and relevant online resources that are on-topic? I like to upvote questions, but my personal criteria requires that effort/research is demonstrated in the question.– mickmackusaCommented Jan 13, 2022 at 19:53
Add a comment
|
2 Answers
You should be able to load it like so:
Joomla\CMS\HTML\HTMLHelper::_('jquery.framework');
Else, if you're using Joomla's Web Asset Manager, then you can define it as a dependency in your JSON object:
{
"name": "foobar",
"type": "script",
"uri": "com_mycomponent/script.min.js",
"dependencies": [
"jquery"
]
},
Lodder answer is certainly useful, but not what I wanted to see. The correct loading of the embedded jquery looks like this.
use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->useScript('jquery');
This will work if jquery has not been loaded before.