Joomla 将脚本从视图添加到头部脚本的底部
Joomla add script from view to bottom of the head scripts
我想添加这个 angular 脚本:
<script src="/sitename/templates/templatename/app/modules/form/form.js"></script>
..从 Joomla Nooku 视图 my-account.html.php 到头部脚本的底部。因为在模板文件中有 $doc->addScript() 用于添加脚本,例如 AngularJS 本身。所以在视图本身中添加的自定义脚本需要在底部。
如何操作?
如果我在视图中使用 AddScript(),脚本将添加到头部脚本的顶部。
我想出了一个解决方案……有点像。在模板文件中,我指定了我想移动到头脚本底部的文件。像这样(欢迎更好的solution/approach):
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>');
我想添加这个 angular 脚本:
<script src="/sitename/templates/templatename/app/modules/form/form.js"></script>
..从 Joomla Nooku 视图 my-account.html.php 到头部脚本的底部。因为在模板文件中有 $doc->addScript() 用于添加脚本,例如 AngularJS 本身。所以在视图本身中添加的自定义脚本需要在底部。
如何操作?
如果我在视图中使用 AddScript(),脚本将添加到头部脚本的顶部。
我想出了一个解决方案……有点像。在模板文件中,我指定了我想移动到头脚本底部的文件。像这样(欢迎更好的solution/approach):
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>');