Search code examples
javascriptphpjoomla

Joomla add script from view to bottom of the head scripts


I would like to add this angular script:

<script src="/sitename/templates/templatename/app/modules/form/form.js"></script>

..to the bottom of the head scripts from the Joomla Nooku view my-account.html.php. Because in the template file there are the $doc->addScript()'s for adding scrips like AngularJS itself. So the custom script added in the view itself needs to be at the bottom.

How to do this?

If I use AddScript() in the view the script get added at the top of the head scripts.

I came up with a solution... well sort of. In the template file I specifiy the files that I would like to move to the bottom of the head scripts. Like this (a better solution/approach is welcome):

moveScriptToBottom($doc->_scripts, '/sitename/templates/templatename/app/modules/form/form.js');

function moveScriptToBottom(&$scripts, $src)
{
    foreach ($scripts as $key => $value) {
        if ($key === $src) {
            $move = $scripts[$key];
            unset($scripts[$key]);
            $scripts[$key] = $move;
        }
    }
}

Solution

  • There's no Joomla native way to do it, so the solution you came up with is more or less the only way to do it.