12
<reference name="footer">
    <action method="addItem">
        <type>skin_js</type>
        <file>js/fabric/tool/controller_tool.js</file>
    </action>
</reference>

I used above code to load js in footer. But Magento throws error Like

Invalid method Mage_Page_Block_Html_Footer::addItem(Array ( [0] => skin_js [1] => js/fabric/tool/controller_tool.js ) )

I need to load js in footer. How To overcome this issue.

8

Currently Magento footer block has not been designed to add javascripts.

I tried a while ago refactoring Magento and the footer block to load every JS in the footer instead of the header but the way the JS call are called inside the templates makes it very hard to get this working.

My suggestion to fix your issue is to update your layout like this:

<reference name="before_body_end">
    <block type="core/template" name="controller_tool_javascript" template="fabric/tool/controller_tool_js.phtml"/>
</reference>

Create a fabric/tool/controller_tool_js.phtml file in your template folder with the following code:

<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/fabric/tool/controller_tool.js') ?>"></script>

Let me know if that works.

11

The footer doesn't have these functionalities, only the head does as this block is of type page/html_head which holds these methods.

You can achieve this by putting the JS <script src=...></script> tag inside a template (.phtml file) and include that as a core/template block:

<reference name="footer">
    <block type="core/template" name="fabric_controller_tool_js" template="fabric/controller_tool_js.phtml" />
</reference>

Also you could add it through a core/text block:

<reference name="footer">
    <block type="core/text" name="fabric_controller_tool_js">
         <action method="setText">
             <text><![CDATA[<script src="/js/fabric/tool/controller_tool.js"></script>]]></text>
         </action>
    </block>
</reference>
2

Just want tell you why addItem not work in reference name="footer"

When you use reference name="footer" then it will call this block

<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml"> 

which you will find in page.xml in your theme.

so it mean it will check addItem method/function in that block class or in their parent class, but this function is not there, that's why it will not work and it will throw exception.

2

<reference name="footer"> does not work in my case. I've found more appropriate block for js called js:

<reference name="js">
    <block type="core/text" name="fabric_controller_tool_js">
        <action method="setText">
            <text><![CDATA[<script src="/js/fabric/tool/controller_tool.js"></script>]]></text>
        </action>
    </block>
</reference>

Or through separate template. path: design/adminhtml/default/default/template/sales/order/js.phtml:

<reference name="js">
    <block type="core/template" name="fabric_controller_tool_js" template="sales/order/js.phtml" />
</reference>
1

I used a different method to achieve what you may be seeking to accomplish. Instead of twisting magento's arm, I just load my script in the head, but set a DOMContentLoaded event listener (not supported in ie8) to which performs my tasks.

app/design/frontend/base/default/layout/namespace_module.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.0.1">
  <default>
    <reference name="head">
      <action method="addJs">
        <script>NameSpace/Module/entry.js</script>
      </action>
    </reference>
  </default>
</layout>

app/code/community/NameSpace/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <NameSpace_Module>
      <version>0.0.1</version>
    </NameSpace_Module>
  </modules>
  <frontend>
    <layout>
      <updates>
        <module>
          <file>namespace_module.xml</file>
        </module>
      </updates>
    </layout>
  </frontend>
</config>

js/NameSpace/Module/entry.js

document.addEventListener("DOMContentLoaded", function(event) {
  // do something
});
1

You can add new block in 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

<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

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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.