when using addScriptDeclaration, the script is added in the first tag that is found inside the HEAD tag.

Is it possible to add it just before </HEAD>?


3 Answers 3


You can do this by using the new feature Child Templates in Joomla 4.1. Step by step details:

  1. System > Site Templates > Cassiopeia Details and Files > and hit "Create Child Template"
  2. Open index.php under /templates/cassiopeia
  3. Select all the code and copy. Then close.
  4. Go to System > Site Templates > and click to the newly created child template
  5. Hit to "New File" and name it index.php
  6. Paste the code.
  7. Open templateDetails.xml and add the following code:
    <field name="myJS" type="textarea" label="My JS script" />
  8. Edit child template index.php file, and add this new line inside <head> </head> tags:
    <script><?php echo $this->params->get('myJS'); ?></script>
  9. Go to System > Site Template Styles and click on the child template
  10. On the Advanced tab you will see a new field My JS script.
    There you can add your custom JS code.

That's all !

All credit goes to Dimitris Grammatiko and his presentation on JUG London !

  • 1
    Welcome at Joomla Stack Exchange. Please take our tour. Could you please add somewhat more information about that new Joomla 4.1 feature so that people know how to use it? Thanks!
    – pe7er
    Commented Feb 16, 2022 at 20:19
  • @marian please edit your answer to be more explanatory about how to implement this feature and possibly how this feature works if you can. Any supporting links/documentation/screenshots will make this answer more helpful to researchers. Commented Feb 16, 2022 at 20:28

Yes you can put in the head, but its most common to put it before the tag

  • The script must be in the template index.php, or you can use Sourcerer from Regular Labs Commented Sep 17, 2020 at 21:35
  • The system has flagged this post as being low-quality (because of its length/content). I think perhaps the OP is asking about how to ensure that the new declaration is appended to the <head></head> as the last child inside of it. In other words, the OP is trying to ensure that the declaration comes after all other declarations in the head tag. Please do not extend your answer's advice by adding comments. When you have additional relevant details to add, please edit your post so that the whole story is in one cohesive place (then delete your comment). Commented Sep 17, 2020 at 22:30

There is a "dirty" way to do this. Use regular labs rereplacer and simply replace in the head and replace

with something before it


The swiss army penknife of the joomla world


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.