2

I've been trying for the last hour or so to get a single javascript added to the head via my system plugin, and am kind of pulling my hair out as to why it is not showing up.

Here's what I've got going in my plugin php file so far:

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

class PlgJoomlaKey extends JPlugin
{
    public function onAfterRender ()
    {
        //only going to run these in the backend for now
        if ($app->isSite()) {
            return;
        }
        $document = JFactory::getDocument();
        $document->addScript(JUri::root().'plugins/system/joomlakey/joomlakey.js' );
    }
}

I'm obviously doing something wrong, but I have no clue what. I've tried as many different functions besides onAfterRender as I can see might work, but none of those seem to change anything. I've tweaked the file path every which way but that doesn't seem to be the problem (besides, it should add it, I'd at least see the (wrong) file path in the head).

I know the plugin is loading, because I can type, say, break right above the comment of //only going to run ... which then breaks my site when I reload - so the plugin is executing, but for some reason that script is not getting placed in the head like it should be.

1
  • I made a small plugin allowing people to add custom CSS and JS files t their site: Feel free to download and/or look through the source code
    – Lodder
    Commented Aug 10, 2015 at 7:34

1 Answer 1

5

As the name of event onAfterRender says:

This event is triggered after the framework has rendered the application.

So application has been already rendered, you can't edit it anymore. You could use onBeforeRender, but since you are adding JS to <head>, most logical would be to use onBeforeCompileHead event.

This event is triggered before the framework creates the Head section of the Document.

And you probably have error reporting off. You haven't defined $app.

Final code

public function onBeforeCompileHead()
{
    //only going to run these in the backend for now
    $app = JFactory::getApplication();
    if ($app->isSite()) {
        return;
    }

    $document = JFactory::getDocument();
    $document->addScript(JUri::root().'plugins/system/joomlakey/joomlakey.js' );
}
2
  • you are totally right, I'm not sure why I didn't realize that :)
    – Jon
    Commented Aug 10, 2015 at 16:42
  • Some days just are like this :)
    – Rene Korss
    Commented Aug 10, 2015 at 17:54

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.