簡體   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