2

In this post: How to add CSS/JS/an Image to a module backend @lodder shows me how to use HTMLHelper to add CSS and JavaScript.

Previously I had followed the Joomla! documentation https://docs.joomla.org/Adding_JavaScript which suggests using addScript()

In the example by Lodder he is using a form field and then is using getInput so I wondered if that makes a difference. I'm keen to learn the right way to do things moving forwards and I know namespacing is what I should be using as a good habit.

My question is instead of using JHhtml for things like

JHtml::script('jui/jquery.min.js', false, true);

to load jQuery first, can I now do

HTMLHelper::_('script', 'jui/jquery.min.js', false, true);

And secondly, instead of adding to the document head like this:

$document = JFactory::getDocument();
$document->addScript($mediaUrl . '/js/' . $moduleName . ".js",
  "text/javascript", true, false, array('version' => 'auto'));

Can I do it like this?

$document = JFactory::getDocument();
$document->HTMLHelper::_('script', 'jui/jquery.min.js', false, true);
4

1) JHtml is an alias for Joomla\CMS\HTML\HTMLHelper class. You'll have to start using the real class sooner or later because the alias will not exist in Joomla! 5.0. To use the real class you have to either import it:

use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::doSomething();
HTMLHelper::doSomethingElse();

Or use fully qualified class name:

\Joomla\CMS\HTML\HTMLHelper::doSomething();
\Joomla\CMS\HTML\HTMLHelper::doSomethingElse();

2) No, that doesn't work. The document object does not have property called HTMLHelper. Use one of the methods shown above.

Note, you can use HTMLHelper::_('jquery.framework') to add core jQuery.

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.