How to load JS in Magento footer?

14 September 2017 489

Sometimes it can be required to load JS in Magento footer. Trying to do you it you will get the following error:


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


By default Magento footer block is not designed to work with javascript. JS works only in header as this block is of type page/html_head which holds these methods


There are 3 ways how you could achieve your goal.


  1. The first option is to update layout as following:


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


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


  1. The second would be to putting the JS <script src=...></script> tag inside a template (.phtmlfile) 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>


  1.  And the third one is to add your JS code 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>