如何在Joomla1.7中使用headjs?
尝试在 Joomla 中使用 HeadJS 。我在模板中添加了一段代码 - 它获取 head 输出中的 javascript 文件,准备一个字符串以在 head.js 代码之间输出它们,然后从 joomla 的 head 输出标记中删除所有 javascript 文件。
问题是一些脚本是在模板之后加载的。例如,模块将在模板加载后将一些脚本文件排入队列,因此其文件出现在我的 head.js 代码之外。我有什么想法可以控制这个吗?
$data = $this->getHeadData();
if( $data['scripts'] ){
foreach ($data['scripts'] as $url=>$type){
if( !strstr($url, 'ajax.googleapis.com/ajax/libs/jquery') )
$headjs[] = $url;
}
unset( $data['scripts'] );
$data['scripts'][$template . '/js/head.js'] = array(
'mime' => 'text/javascript',
'defer' => false,
'async' => false);
$this->setHeadData($data);
}
进而...
<script>
head.js(
<? foreach($headjs as $script): ?>
'<?=$script?>',
<? endforeach; ?>
function(){
}
);
</script>
Trying to use HeadJS in Joomla. I added a code to my template - it grabs the javascript files in the head output, prepares a string to output them between head.js code, then removes all javascript files from joomla's head output tag.
The problem is that some scripts are loaded after the template. For example, a module will enqueue some script files after the template has loaded, so its files appear outside of my head.js code. Any ideas how I can control this?
$data = $this->getHeadData();
if( $data['scripts'] ){
foreach ($data['scripts'] as $url=>$type){
if( !strstr($url, 'ajax.googleapis.com/ajax/libs/jquery') )
$headjs[] = $url;
}
unset( $data['scripts'] );
$data['scripts'][$template . '/js/head.js'] = array(
'mime' => 'text/javascript',
'defer' => false,
'async' => false);
$this->setHeadData($data);
}
And then...
<script>
head.js(
<? foreach($headjs as $script): ?>
'<?=$script?>',
<? endforeach; ?>
function(){
}
);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不同的扩展可以连接到处理输出期间触发的不同事件(因此即使在呈现模板之后)。
到目前为止,我发现的最佳选择是创建系统插件并将其顺序移至最后一个可用的系统插件。
现在你有两个选择:
如果您在完全控制网站的情况下使用异步脚本加载,这很好,但几乎不可能实现为 Joomla 的通用扩展。有些扩展在 head 中使用内联脚本,有些在 html body 中使用内联脚本,您必须保留执行顺序(简单的示例:Mootools 必须首先加载)。
我通常只异步加载那些我自己包含的脚本(在我的模板或扩展中)。任何使用 Joomla 核心(Mootools、core.js 等)或其他扩展添加的脚本我不碰。
Different extensions can hook up to different events that are triggered during processing the output (so even after template is rendered).
Best option I found so far is creating system plugin and moving it's order to be the last of available system plugins.
Now you have two options:
This is fine if you play around with async scripts loading where you have total control over the website, but it's almost impossible to implement as universal extension for Joomla. Some extensions use inline scripts in head, some in the the html body and you'd have to preserve order of execution (simple example: Mootools have to load first).
I use to load asynchronously only those scripts which I've included myself (in my template or my extension). Any scripts that are added with Joomla core (Mootools, core.js, etc.) or other extensions I don't touch.