我在一个magento模板中加载css和js,在page.xml中加载这样的东西:
<default>
<block type="page/html" name="root" output="toHtml" template="page/1column.phtml">
<block type="page/html_head" name="head" as="head">
<action method="addCss"><stylesheet>css/global_1.2.8.css</stylesheet></action>
<action method="addJs"><script>jquery/jquery-1.7.js</script></action>
<action method="addJs"><script>colorbox/jquery.colorbox-min.js</script></action>
<action method="addJs"><script>compressed/base-1.4.js</script></action>
并在head.phtml中
<?php echo $this->getCssJsHtml() ?>
但是当我查看源代码或观察firebug中的Net选项卡时,我看到js在css之前被加载了。是什么导致这种情况,我怎么能纠正?我知道我可以将js移动到页脚,但我认为这会引起其他问题所以我想把它全部保留在头部,只是按照正确的顺序。
我想我会更新这个以反映罗马人的回答。我检查了我正在使用的Magento版本中的方法,1.3.2.3。升级不是一个选项,所以这可能只是该版本的错误?
# app/code/core/Mage/Page/Block/Html/Head.php
public function getCssJsHtml()
{
// return '';
$lines = array();
$baseJs = Mage::getBaseUrl('js');
$html = '';
$script = '<script type="text/javascript" src="%s" %s></script>';
$stylesheet = '<link rel="stylesheet" type="text/css" href="%s" %s />';
$alternate = '<link rel="alternate" type="%s" href="%s" %s />';
foreach ($this->_data['items'] as $item) {
if (!is_null($item['cond']) && !$this->getData($item['cond'])) {
continue;
}
$if = !empty($item['if']) ? $item['if'] : '';
switch ($item['type']) {
case 'js':
#$lines[$if]['other'][] = sprintf($script, $baseJs.$item['name'], $item['params']);
$lines[$if]['script'][] = $item['name'];
break;
case 'js_css':
//proxying css will require real-time prepending path to all image urls, should we do it?
$lines[$if]['other'][] = sprintf($stylesheet, $baseJs.$item['name'], $item['params']);
#$lines[$if]['stylesheet'][] = $item['name'];
break;
case 'skin_js':
$lines[$if]['other'][] = sprintf($script, $this->getSkinUrl($item['name']), $item['params']);
break;
case 'skin_css':
$lines[$if]['other'][] = sprintf($stylesheet, $this->getSkinUrl($item['name']), $item['params']);
break;
case 'rss':
$lines[$if]['other'][] = sprintf($alternate, 'application/rss+xml'/*'text/xml' for IE?*/, $item['name'], $item['params']);
break;
}
}
foreach ($lines as $if=>$items) {
if (!empty($if)) {
$html .= '<!--[if '.$if.']>'."\n";
}
if (!empty($items['script'])) {
$scriptItems = array();
if (Mage::getStoreConfigFlag('dev/js/merge_files')) {
$scriptItems = $this->getChunkedItems($items['script'], 'index.php?c=auto&f=');
} else {
$scriptItems = $items['script'];
}
foreach ($scriptItems as $item) {
$html .= sprintf($script, $baseJs.$item, '') . "\n";
}
// foreach (array_chunk($items['script'], 15) as $chunk) {
// $html .= sprintf($script, $baseJs.'index.php/x.js?f='.join(',',$chunk), '')."\n";
// }
}
if (!empty($items['stylesheet'])) {
foreach ($this->getChunkedItems($items['stylesheet'], $baseJs.'index.php?c=auto&f=') as $item) {
$html .= sprintf($stylesheet, $item, '')."\n";
}
// foreach (array_chunk($items['stylesheet'], 15) as $chunk) {
// $html .= sprintf($stylesheet, $baseJs.'index.php/x.css?f='.join(',',$chunk), '')."\n";
// }
}
if (!empty($items['other'])) {
$html .= join("\n", $items['other'])."\n";
}
if (!empty($if)) {
$html .= '<![endif]-->'."\n";
}
}
return $html;
}
答案 0 :(得分:0)
这个顺序css然后js硬编码在这里app / code / core / Mage / Page / Block / Html / Head.php 请参阅方法公共函数getCssJsHtml()
正如你所看到的那样
// static and skin css
$html .= $this->_prepareStaticAndSkinElements('<link rel="stylesheet" type="text/css" href="%s"%s />' . "\n",
empty($items['js_css']) ? array() : $items['js_css'],
empty($items['skin_css']) ? array() : $items['skin_css'],
$shouldMergeCss ? array(Mage::getDesign(), 'getMergedCssUrl') : null
);
// static and skin javascripts
$html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
empty($items['js']) ? array() : $items['js'],
empty($items['skin_js']) ? array() : $items['skin_js'],
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
);
// other stuff
所以,如果你真的需要改变这个顺序 - 你需要覆盖这个类并自己做订单。
祝你好运。