简体   繁体   English

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

[英]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. ..从Joomla Nooku的头部脚本底部开始,查看my-account.html.php。 Because in the template file there are the $doc->addScript()'s for adding scrips like AngularJS itself. 因为在模板文件中有$ doc-> addScript()用于添加脚本(例如AngularJS本身)。 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. 如果在视图中使用AddScript(),则脚本将添加到头脚本的顶部。

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. 在模板文件中,我指定了要移至head脚本底部的文件。 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>');

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

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