Adding jQuery to Magento

Adding jQuery to Magento

July 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding jQuery to Magento

Adding jQuery Before Magento Scripts Using local.xml

When working with Magento, you might find yourself needing to add custom scripts like jQuery before the default scripts that come bundled with the platform. While this seems straightforward, many developers struggle with getting the order of scripts right.

Understanding the Issue

The challenge arises because Magento’s page layout is defined in page.xml, which adds a series of scripts to the head section. If you try to add jQuery using local.xml as shown below:

<reference name="head">
    <action method="addItem">
        <type>skin_js</type>
        <script>js/jquery-1.6.4.js</script>
    </action>
</reference>

You might expect it to be added before the other scripts, but it ends up being appended at the end due to Magento’s internal processing of script items.

The Wrong Approach: Removing and Re-adding Scripts

Some developers try to resolve this by removing all the scripts from page.xml using an action like:

<action method="removeItem">
    <type>skin_js</type>
    <script>js/jquery-1.6.4.js</script>
</action>

Then, they re-add them in local.xml in the desired order. However, even when this is done correctly, the scripts still end up being added in the wrong order.

The Correct Approach: Using a Custom Block

The key to solving this problem lies in Magento’s block system and its event-driven architecture. Instead of trying to modify the existing head block or remove and re-add scripts, you should create a new block that inherits from Mage_Page_Block_Html_Head and override the _toHtml() method.

Here’s an example:

  1. Create your custom block in /app/code/local/YourNamespace/YourModule/etc/config.xml:
<config>
    <global>
        <blocks>
            <yourmodule>
                <class>YourNamespace_YourModule_Block</class>
            </yourmodule>
        </blocks>
    </global>
</config>
  1. Create the block file at /app/code/local/YourNamespace/YourModule/Block/Yourcustomhead.php:
<?php

class YourNamespace_YourModule_Block_YourCustomHead extends Mage_Page_Block_Html_Head
{
    protected function _toHtml()
    {
        $this->addItem('js/jquery-1.6.4.js', 'js/jquery-1.6.4.js');

        return parent::_toHtml();
    }
}
  1. In system.xml, add a new field for your custom head block:
<fields>
    <head_block translate="label" module="yourmodule">
        <label>Your Custom Head Block</label>
        <rewrite>true</rewrite>
        <backend_model>Mage_Core_Model_Config_Scope_Code</backend_model>
        <sort_order>100</sort_order>
    </head_block>
</fields>
  1. In page.xml, replace the default head block with your custom one:
<reference name="root">
    <block type="yourmodule/yourcustomhead" name="head" as="head">
        <action method="setTemplate">
            <template>page/html/head.phtml</template>
        </action>
    </block>
</reference>

Conclusion

Adding scripts before Magento’s default ones requires a deep understanding of the platform’s block system and event dispatching. By creating your own custom head block that inherits from Mage_Page_Block_Html_Head and overrides the _toHtml() method, you can ensure that your script is added to the page in the correct order.

Remember, modifying core files directly is generally discouraged due to the risks it poses during upgrades. Always try to extend the core functionality instead of replacing it.

For more advanced techniques on working with Magento’s block system and event-driven architecture, consider reading our detailed guide here.

Database Themes Configuration Orders

Our network