I am trying to use JavaScript to add a class to the body which is the same as the current menu items alias.
I have echoed $prefix
and it is correct. So I know that part works.
I have dumped the JS into the console (minus the dynamic variable) and that works. So I think that works too.
So now it appears it simply doesn't work in this plugin. But I copied the plugin from another one I made, and that works.
Can anyone explain why this doesn't add the alias to the body class?
jimport( 'joomla.plugin.plugin' );
class plgSystemKouAliasAsBodyClass extends JPlugin
{
function __construct( $subject, $params )
{
parent::__construct($subject, $params);
}
function onBeforeCompileHead()
{
$app = JFactory::getApplication();
// If we are on admin don't process.
if (!$app->isClient('site'))
{
return;
}
$document = JFactory::getDocument();
$menu = $app->getMenu();
$active = $menu->getActive();
$alias = ucfirst($active->alias);
if (!empty($prefix))
{
$alias = $prefix . $alias;
}
// Does not work, although $alias is correct when echoed.
$document->addScriptDeclaration('
document.addEventListener("DOMContentLoaded", function() {
document.body.classList.add("' . $alias . '");
});
');
// Does not work, although document.body.classList.add("EoinsClass");
// works when used in the console. And the rest works when
// used in a different plugin.
$document->addScriptDeclaration('
document.addEventListener("DOMContentLoaded", function() {
document.body.classList.add("EoinsClass");
});
');
}
}
$active
isnull
on such pages.