Joomla将脚本从视图添加到头部脚本的底部


Joomla add script from view to bottom of the head scripts

我想添加这个角脚本:

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

. .从Joomla Nooku查看my-account.html.php的头部脚本的底部。因为在模板文件中有$doc->addScript()用于添加脚本,比如AngularJS本身。视图中添加的自定义脚本需要在底部。

如何做到这一点?

如果我在视图中使用AddScript(),脚本将被添加到头部脚本的顶部。

我想出了一个解决办法…差不多吧。在模板文件中,我指定了想要移到头部脚本底部的文件。像这样(欢迎更好的解决方案/方法):

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

Joomla本身没有办法做到这一点,所以您提出的解决方案或多或少是唯一的方法。

这肯定会成功的

<?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);
?>

同样的,你可以通过

删除一些不需要的脚本

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

您可以通过使用"addCustomTag"来做到这一点,虽然它不能保证脚本标记将被添加到页面的末尾,但它仍然可以解决Joomla的许多序列问题。

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