Joomla异步脚本加载jdoc头
问题描述:
我正在使用Joomla! 3.4Joomla异步脚本加载jdoc头
<jdoc:include type="head">
调用head.php
文件/libraries/joomla/document/html/renderer/
,其中$strAttr['async']
的引用可以在生成脚本文件链接的部分找到。
$strAttr
只是由foreach循环命名的数组,但它来自$document->_scripts
。
我想异步加载我的脚本,如何为每个脚本文件更改属性$strAttr['async']
?我知道我只需更改head.php
中的代码,但我认为我忽略了Joomla中的某些设置。
答
如果您想要为每个脚本文件执行此操作,您必须编写一个系统插件并实现函数onBeforeCompileHead()
。
在这个函数中,你需要类似的东西:
/**
* possible implementation of onBeforeCompileHead
* to make all the scripts async.
*/
public function onBeforeCompileHead() {
$document = JFactory::getDocument();
foreach ($document->_scripts => $url) {
$document->_scripts[$url]['async'] = true;
}
}
相反,如果你只想要让您的应用程序JDocument :: addScript或模板的使用JS();像这样:
/**
* Possible implementation to insert a async script in a component or a module.
* @see JDocument::addScript($url, $type = "text/javascript", $defer = false, $async = false)
*/
$document = JFactory::getDocument();
$document->addScript('/media/system/js/sample.js', "text/javascript", false, true);
嘿那里。我从一开始就重新调整了您的问题以说明上下文,并略微改进了代码格式。不要犹豫,把一些大胆的地方,我不知道同步部分会在这个问题。祝你好运!您可以在此处添加生成脚本文件链接的渲染器的一部分,以帮助读者帮助您。 – 2015-03-30 22:33:33