3

I'm using a lot of javascript files for my articles. These scripts shouldn't be loaded everywhere - only in certain articles. That's the reason why I don't add them inside of the templates´ head. At the moment I've included them directly inside the article.

Do I have any disadvantages (regarding SEO) when these scripts aren't placed in Joomla's header?

What is the best solution to place article specific scripts inside the header area?

0

2 Answers 2

2

There are a few other ways to outline for what you're asking about.

SEO Considerations

If you run Google Lighthouse on your site to measure the performance, you may find that there may be performance and best practice recommendations to delay loading of render-blocking javascript. As a result, loading your custom JS in the head may cause a performance issue, so loading it later is recommended.

Loading it in the article

As @pe7er notes, by default you can not directly add PHP code, and sometimes JS code into the article, depending on your site's editor configuration.

For example, if using JCE as your editor, you can modify the editor profile to allow PHP and JS code to be included. That's not recommended though as it can create security issues, or potentially white screen errors if your syntax is wrong.

There are some 3rd party extensions I use that can assist with inserting code, such as Regular Labs Sourcerer, where you can then use a shortcode to wrap around PHP code which is then deactivated in the editor, but will render the code in the page.

Loading it in the footer or debug position

Rather than needing to repeatedly remember to add the code in the article, the third place I would look to load the code is via a module in either a footer module position, or the debug position.

The debug position would load it just before the closing body and html tags. This would delay the loading for performance purposes.

I'd also look at assigning that module to either the category that you need the JS to load for (if it's common to all articles in a category), assign it to specific pages or articles, or assign it based on other rules. You might need Regular Labs Advanced Module Manager in order to extend your options on how to assign the module to particular subsets of content.

1

You could add a script (or CSS) to the doc head using the HTML Helper

use Joomla\CMS\HTML\HTMLHelper;

HTMLHelper::_('script', 'path/to/your/js/script.js', 
['version' => 'auto', 'relative' => false]
);

However, you cannot add PHP code to Joomla articles so you have to use another method for that. For instance a plugin that adds it to the code for certain articles (article IDs).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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