pavsid
pavsid

Hi all, I've created a plugin which needs to inject some raw javascript into the head section. I can include the raw js via a partial with something like {% partial 'MyPlugin/head-script' %} (unless you can recommend a better way), but the important thing is that it only gets rendered/output when the component is active on the current page. How might I go about doing that?

Last updated

Adrien - ACTE Solutions
Adrien - ACTE Solutions

Hello, Your script file should be in your-plugin-folder/assets/js/head-script.js

In your component add onRun() function:

public function onRun()
{
    $this->addJs('/plugins/your-plugin-folder/assets/js/head-script.js');
}

Your layout should have the {% scripts %} tag too.

See documentation: https://octobercms.com/docs/plugin/components#component-assets

1-2 of 2