Michel Arteta
Michel Arteta

Reputation: 1384

Magento add Js to the footer

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!

code used in local.xml: 

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

Upvotes: 8

Views: 21183

Answers (2)

Marcio Maciel
Marcio Maciel

Reputation: 71

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

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

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

2 - Add this html node to your layout 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!

Upvotes: 5

Jason
Jason

Reputation: 1962

Referencing my answer here:
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:

<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:

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

That should do it.

Upvotes: 1

Related Questions