What is the best way how to add jquery code by Joomla plugin?

How can I set that these code should be applied just for checkout page of VirtueMart in xml file?

Is this the best way?


// no direct access
defined('_JEXEC') or die;

class plgContentHelloworld extends JPlugin
    public function onAfterRender() {
        echo '<script>';
            echo 'alert("Hello World!");';
        echo '</script>';

  • Welcome to JSE. Please take our tour while you wait for support/feedback. Aug 18, 2021 at 12:21

2 Answers 2


If you are using Joomla 4 you can do it via WebAsset Manager, too: https://docs.joomla.org/J4.x:Web_Assets

JQuery is shipped with Joomla 4. You do not need to add the library yourself; https://docs.joomla.org/Potential_backward_compatibility_issues_in_Joomla_4#jQuery

  • Thank you for answer. Can I create js file just inside my plugin folder, for example I will have my_plugin.php, my_plugin.xml and my_plugin.js. And then I can use this scripts from my_plugin.js for example for checkout page connected via this code: $jinput = JFactory::getApplication()->input; $option = $jinput->get('option'); $view = $jinput->get('view'); $task = $jinput->get('task'); if ($option == 'com_virtuemart' && $view == 'cart') { echo 'blah'; } Aug 19, 2021 at 13:54
  • I haven't looked in the code, but according to the documentation, Joomla 4's WebAssetManager only looks in the media or template folder. See: docs.joomla.org/J4.x:Web_Assets#Register_an_asset
    – astridx
    Aug 20, 2021 at 9:24

If you want to use a plugin then you should replace the echo/script you have with code that adds it to the header using the examples here, https://docs.joomla.org/Adding_JavaScript

Or use a 3rd party plugin like Sourcerer from regular labs to do it for you, https://regularlabs.com/sourcerer

  • Thank you for answer, but I read in this artice that it is possible to use inline javascript just inside component view folder. Is there any way how can I do it inside my my_plugin.php? Aug 19, 2021 at 13:47
  • Your plugin is running on the server side, your JS in a view is on the client/browser side so a plugin can put JS in a page on the client side but it can't run JS itself. Perhaps you should accept one of these answers to this question and then create a new question that shows the actual JS you have written that you would like to appear on your checkout page and provide more background about what you are trying to achieve and then you might receive more specific help and possibly an example of what you need to do.
    – Irata
    Aug 19, 2021 at 22:08

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.