My preferred (and most flexible way) to do this is via XML, using local.xml
, two separate Javascript files, and a new file that we will create. The first Javascript file is jQuery itself - completely unmodified. The second file, which I call no-conflict.js
, contains only one line:
jQuery.noConflict();
Now we will add both of these files along with the new block to the header section of our local.xml
:
<reference name="head"> <block type="page/html_head" name="topScripts" template="page/html/top_scripts.phtml" before="head"> <action method="addItem"> <type>skin_js</type> <name>js/jquery-1.7.2.min.js</name> </action> <action method="addItem"> <type>skin_js</type> <name>js/no-conflict.js</name> </action> </block> </reference>
no-conflict.js
requires jQuery to work with Prototype, the Javascript framework included in Magento by default. Saving the jQuery and no-conflict
sections allows you to update or lower jQuery as needed without having to edit the jQuery file itself to include the noConflict()
method.
In our XML, we created a new block ( topScripts
) with a template file set to top_scripts.phtml
.
Go to /app/design/frontend/PACKAGE_NAME/TEMPLATE_NAME/template/page/html/
and create this new file. It will contain one line:
<?php echo $this->getCssJsHtml(); ?>
Now edit /app/design/frontend/PACKAGE_NAME/TEMPLATE_NAME/template/page/html/head.phtml
.
Find the line <?php echo $this->getCssJsHtml() ?>
In head.phtml
and add this line directly above it:
<?php echo $this->getChildHtml('topScripts') ?>
You have now correctly added jQuery in front of any other Magento Javascript.
source share