首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是什么导致我的脚本在magento的css之前加载?

是什么导致我的脚本在magento的css之前加载?
EN

Stack Overflow用户
提问于 2012-01-31 16:41:22
回答 1查看 1.5K关注 0票数 4

我在magento模板中加载css和js,在page.xml中使用如下内容:

代码语言:javascript
运行
复制
<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中

代码语言:javascript
运行
复制
<?php echo $this->getCssJsHtml() ?>

但是,当我查看源代码或查看firebug中的Net选项卡时,我看到js正在被加载到css之前。是什么导致了这一切,我怎么纠正呢?我知道我可以把js移到页脚,但是我认为这会引起其他的问题,所以我想把它放在脑中,保持正确的顺序。

我想我应该更新一下,以反映罗马人的回答。我检查了我正在使用的Magento版本中的方法1.3.2.3。升级不是一种选择,所以也许这只是一个带有该版本的bug?

代码语言:javascript
运行
复制
# 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&amp;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&amp;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;
    }
EN

回答 1

Stack Overflow用户

发布于 2012-01-31 19:06:12

这个命令js然后硬编码在这里-- app/code/core/Mage/Page/Block/Html/Head.php参见方法公共函数getCssJsHtml()

就像你看到的

代码语言:javascript
运行
复制
// 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

所以,如果您真的需要更改这个顺序--您需要覆盖这个类并做出自己的命令。

祝好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9083322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档