Adding Arbitrary Code to your <head> in Magento

Written by Ben Wendt

Magento’s built in addJs method in the Mage_Page_Block_Html_Head class assumes that files will be hosted locally. This isn’t always desirable. E.g. you may want to use Google’s CDN to host jQuery.

This can be accomplished by adding the following to your layout xml:

<reference name="head">
            <block type="core/text" name="my_head"></block>
        </reference>

Now, in your controller you can add anything into this block that you want, like so:

$google_url = 'http://google.com/jquery.js';
$this->getLayout()->getBlock('my_head')->setText("

            <script src='$google_url'></script>

        ");