Magento 1 Performance – Move All Javascript Includes to Before

javascriptlayoutmagento-1performancetheme

Does anyone know how to have all of Magento's JS script tags e.g. <script type="text/javascript" src="http://sitename.com/js/prototype/prototype.js"></script> render out before the closing </body>?

I've tried this once before, but I was given an error which I think was along the lines of the addJS method not being available where I used it, possibly in reference footer.

Best Answer

It depends on your request. For instance, lastly, I had been removed all Prototype scripts from the Homepage of the Magento store which I didn't face any problem. But as I said, it depends on your theme, extensions etc.

To moving the script :

Find the following line in page.xml of your theme

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

And insert the following just before :

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

For Magento 1.9 use this:

<block type="page/html_head" name="jsfooter" as="jsfooter" template="page/html/jsfooter.phtml">
       <action method="addItem"><type>skin_js</type><name>js/yourskinfile.js</name><params/></action>
    </block>

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

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

Add below in your template just before closing </body> tag.

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