繁体   English   中英

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(),则脚本将添加到头脚本的顶部。

我想出了一个解决方案…… 在模板文件中,我指定了要移至head脚本底部的文件。 这样(欢迎使用更好的解决方案/方法):

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

这一定会做到的

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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