1

I've just read this tutorial https://docs.joomla.org/J3.x:Developing_an_MVC_Component and finished my component (Joomla! 3.7). Now I realise that the site view doesn't contain the jquery link(s) in the header - in contrast to each other (sub-)page of my site.

I know I could just add the neccessary lines of code in my template, but I didn't do so because right from the beginning Joomla did that for me - except when I browse to my component's view. Unfortunately I couldn't find any info about this behaviour so I hope someone can shed some light on this ...?!

2

It's not up to the template or Joomla to load jQuery. You'll normally find jQuery being loaded on a lot of the default pages cause an extension does, however Joomla is not required to load it by default on every single page, therefore it's up to you.

To load it, simple add:

JHtml::_('jquery.framework');

in your component views.

  • Thanks Lodder! Can I just put this line at the top of my template's index.php to make sure jquery is available on every page? I'm just wondering if it might get loaded multiple times when I do so. – Rico May 16 '17 at 16:30
  • 1
    You can but I wouldn't suggest doing so. Only load jQuery where you need it. If it's for your own site, perhaps use the jQuery Easy plugin. – Lodder May 16 '17 at 16:32
1

To expand on Lodder's answer, using

JHtml::_('jquery.framework');

at the top of your component's views and before any other calls to your custom js, will ensure that jQuery will be loaded, before your other scripts, and only once - even if other extensions or the template have called it with the same method.

Also it worths noting that JHtml::_('jquery.framework'); will load the jQuery in no conflict mode.

To load it in normal mode:

JHtml::_('jquery.framework', false);
1

Joomla adds it all over the place. Do a search for JHtml::_('jquery.framework'); in the core files and see how.

The javascript frameworks do seem a bit messily implemented. There is some information about them here. https://docs.joomla.org/J3.x:Javascript_Frameworks

The most common place seems to be in your component's view default.php file. A "what do Joomla do" approach is usually best if you are unsure of something.

If you just want it available anywhere (and your component is only to be used with your template), then I can't see anything really wrong with adding it to your template, though sometimes this can mean the files load in the wrong order and you have to add it to your component as well to make sure your compoenent's js is loaded after the JQuery.

As it will only be loaded once, there is nothing really wrong in adding it to your template and your component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.