简体   繁体   中英

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;
        }
    }
}

This will surely do it

<?php 
         //get the array containing all the script declarations
         $document = JFactory::getDocument(); 
         $headData = $document->getHeadData();
         $scripts = $headData['scripts'];

         //remove or add your script
         $scripts['/sitename/templates/templatename/app/modules/form/form.js']
         $headData['scripts'] = $scripts;
         $document->setHeadData($headData);
?>

Same way you can remove some un-wanted scripts by this

unset($scripts['/media/system/js/mootools-core.js']);

没有Joomla本机方法,因此您想出的解决方案或多或少是唯一的方法。

您可以使用“ addCustomTag ”来做到这一点 ,尽管它不能保证将脚本标签添加到页面的末尾,但它仍然可以解决Joomla的许多序列问题。

$doc->addCustomTag('<script src="' . JURI::root(true) . '/js/yourscript.min.js" type="text/javascript"></script>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM