Magento: Add Javascript to Home Page Only (Or Any CMS Page)

Magento Icon

Have you ever needed to include a javascript file to the home page only to run that sweet slideshow you have displayed (or any other reason)? Fortunately, you don’t have to modify any of the layout files, or add it to your head.phtml file. CMS pages in Magento allow you to modify the layout XML specific to that page. So, all you need to do is open up the CMS page in Magento that you want to add the javascript to, click on the “Custom Design” tab on the left, and in the “Layout Update XML” field add the following:

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

Or, if you want to just put the javascript in the /js/ directory of your app, you can change the “skin_js” to just “js”.

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Magento: Add Javascript to Home Page Only (Or Any CMS Page)

  1. That’s a really useful tip. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">