将外部javascript文件添加到magento标头

时间:2012-05-23 07:47:00

标签: magento php

我正在尝试将一些外部javascript文件添加到magento cms中,但不知何故它们似乎无法正常工作,尽管它们在头部显示正常。

我在 head.phtml

中添加以下代码行
<!--pankaj js edition-->

<script src="<?php echo $this->getJsUrl(); ?>jQuery_1205141001.js" type="text/javascript"></script>
<script src="<?php echo $this->getJsUrl(); ?>Common_1205141001.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>

当呈现页面时,它们显示在head部分中,但似乎没有做他们应该做的工作。

是的,我做错了什么。因为这些文件在简单的html页面中运行良好。我是否需要在其他地方添加文件。很抱歉打扰我,但我是magento的新手。

3 个答案:

答案 0 :(得分:2)

可能有2个问题

1)您正在尝试在jQuery_1205141001.js中执行jquery,它包含在第一行中,您将在后面包含jquery库。 2)你的jquery与原型相冲突。为此,您需要在执行任何jquery代码之前和在包含jquery库之后在phtml中添加它

<script type="text/javascript">
var $j = jQuery.noConflict();
</script>

然后使用$ j.function代替$ .function for jquery e.g。

$j(document.documentElement).keyup(function (event) {

  if (event.keyCode == 37) {

    $j('#prev1').click();
  } else if (event.keyCode == 39) {
    $j('#next1').click();
  }
});

而不是

$(document.documentElement).keyup(function (event) {

  if (event.keyCode == 37) {

    $('#prev1').click();
  } else if (event.keyCode == 39) {
    $('#next1').click();
  }
});

答案 1 :(得分:1)

不是直接编辑head.phtml文件来添加你的JS引用,你应该把它放在你的主题local.xml中,如下所示:

<reference name="head">
    <block type="core/text" name="google.cdn.jquery" after="-">
        <action method="setText">
            <text><![CDATA[<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
            <script type="text/javascript">jQuery.noConflict();</script>]]>
            </text>
        </action>
    </block>
    <action method="addItem"><type>skin_js</type><script>js/jQuery_1205141001.js</script></action>
</reference>

还要检查Firebug的Javascript错误。如果有javascript错误导致某些js功能失败,firebug会告诉你。

答案 2 :(得分:0)

<reference name="head">
      <action method="addJs"><script>[MODULE]/your.js</script></action>

    </reference>

在magento root的js文件夹中创建与模块名称相同的文件夹,并将js文件复制到此文件夹中。