javascript Magento 将 Js 添加到页脚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15984022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:48:54  来源:igfitidea点击:

Magento add Js to the footer

javascriptxmlmagento

提问by Michel Arteta

I am trying to load js file in the footer of my magento. I am using this code but it give me back an error. Any help? Thanks a lot!

我正在尝试在我的 magento 的页脚中加载 js 文件。我正在使用此代码,但它给了我一个错误。有什么帮助吗?非常感谢!

code used in local.xml: 

<layout>
<default>
    <reference name="footer">
        <action method="addJs"><script>js/file.js</script></action>
    </reference>
</default>
</layout>

回答by Bhavesh Godhani

Find page.xmlof your theme and find the following

找到page.xml您的主题并找到以下内容

<block type="core/text_list" name="before_body_end" as="before_body_end" translate="label">

And insert the following code after that

然后插入以下代码

<block type="page/html_head" name="jsfooter" as="jsfooter" template="page/html/jsfooter.phtml">
   <action method="addJs"><script>your_script.js</script></action>
</block>

Create the template file in app/design/frontend/[package]/[theme]/template/page/html/jsfooter.phtmland put the following:

在其中创建模板文件app/design/frontend/[package]/[theme]/template/page/html/jsfooter.phtml并输入以下内容:

<?php echo $this->getCssJsHtml() ?>

In your page template files “1column.phtml”, “2columns-left.phtml”, “2columns-right.phtml”, “3columns.phtml” and etc. you will need to output this block before tag:

在你的页面模板文件“1column.phtml”、“2columns-left.phtml”、“2columns-right.phtml”、“3columns.phtml”等中,你需要在标签之前输出这个块:

<?php echo $this->getChildHtml('jsfooter') ?>

回答by Marcio Maciel

For Magento v1.6+ (need to test in older versions);

对于Magento v1.6+(需要在旧版本中测试);

1 - create an template file in page/html/footer/extras.phtmlwith this content:

1 -page/html/footer/extras.phtml使用以下内容创建模板文件:

<?php echo $this->getCssJsHtml() ?>

2 - Add this html node to your layout xml:

2 - 将此 html 节点添加到您的布局 xml:

<reference name="before_body_end">
<block type="page/html_head" name="extra_js" as="extraJs" after="-" template="page/html/footer/extras.phtml">
    <action method="addItem"><type>skin_js</type><name>js/jquery.min.js</name></action>
</block>

3 - That is it!

3 - 就是这样!

回答by Jason

Referencing my answer here:
How to add Javascript files in body part (not header) through layout xml files in magento

在这里引用我的答案:
How to add Javascript files in body part (not header) through layout xml files in magento

Your best bet is to make a .phtml file with your js links and add it to your footer using this format:

最好的办法是使用 js 链接制作一个 .phtml 文件,并使用以下格式将其添加到页脚中:

<layout>
    <default>
        <reference name="footer">
            <block type="core/template" name="unique_name_here" template="path/to/js.phtml" />
        </reference>
    </default>
</layout>

For references like this, the footer will automatically load all child html blocks. Parent .phtml template files may need a getChildHtml call in them to be shown, like so:

对于这样的引用,页脚将自动加载所有子 html 块。父 .phtml 模板文件可能需要在其中调用 getChildHtml 才能显示,如下所示:

<?php echo $this->getChildHtml('unique_name_here'); ?>

That should do it.

那应该这样做。

回答by Rahul Dadhich

You can add new block in page.xml

您可以在 page.xml 中添加新块

<block type="core/text_list" name="before_body_end" as="before_body_end" translate="label">
    <block type="page/html_head" name="footerjscss" as="footerjscss" after="-" template="page/html/footerjscss.phtml"/>
</block>

then add JS & CSS files in any layout.xml

然后在任何 layout.xml 中添加 JS 和 CSS 文件

<reference name="footerjscss">
    <action method="addItem"><type>skin_js</type><name>js/slideshow.js</name></action>
    <action method="addItem"><type>skin_css</type><name>css/madisonisland.css</name><params/><if/></action>
</reference>

Create .phtml file in page/html/footerjscss.phtml and add following

在 page/html/footerjscss.phtml 中创建 .phtml 文件并添加以下内容

<?php echo $this->getCssJsHtml() ?>

Now call block in page template “3columns.phtml” and etc. you will need to output this block before tag:

现在在页面模板“3columns.phtml”等中调用块,你需要在标签之前输出这个块:

<?php echo $this->getChildHtml('before_body_end') ?>

Refer code here:http://blog.rahuldadhich.com/magento-load-css-js-footer/

请参阅此处的代码:http : //blog.rahuldadhich.com/magento-load-css-js-footer/