views:

54

answers:

3

In magento I know how to include js files in the page layout file. How though, do i include specific javascript files only on certain pages with my custom module. For example i am writing a custom module that will work with the product view and list pages. I therfore want to have some sort of layout update that i can use with my module that will include my javascript file only on the product view and list pages.

+1  A: 

What you need to do is find the layout handles for the pages that you want to include the JS, and add them to your custom module's layout XML. For instance, to include the JS on the product view, you would add the following node to your layout:

<catalog_product_view>
  <reference name="head">
    <action method="addJs"><script>path/file.js</script></action>  **OR**
    <action method="addItem"><type>skin_js</type><name>path/file.js</name></action>
  </reference>

Choose either option depending on where your JS files live. Have a look in the default catalog.xml or review.xml for examples.

To work out the layout handle (e.g. catalog_product_view), you can use Alan Storm's excellent (and free) LayoutViewer module from his blog.

HTH, JD

Jonathan Day
Thats kind of what i have been doing but it aint working. Will magento load all layout.xml files it finds in the current theme and process them? For example say i just dump a layout file in the base/default theme called mymodule.xml and include a reference as above, will magento process that?
Jon
If you want to quickly through something in a theme layout then call the file local.xml, Magento will always check for that last. I find it especially useful for taking advantage of inheritance in non-default themes.
clockworkgeek
@Jon, as long as there is not a `mymodule.xml` in the current theme/layout dir. I suggest you install the LayoutViewer, that will tell you what layout updates have been applied, so you can check if Magento is finding your xml and the relevant nodes. @clockworkgeek - That's probably not a good approach for a custom module developer as it may conflict with other modules/extensions.
Jonathan Day
did this work for you? Feel free to accept the answer if it did...
Jonathan Day
+1  A: 

Hi Jon, you will need to add a layout update section to your modules config file. based on what you have said you will need something like this in your layout file:

<?xml version="1.0"?>

<layout version="0.1.0">
    <catalog_product_view>
         <reference name="head">
                <action method="addJs"><script>yourscript.js</script></action>
            </reference>
    </catalog_product_view>

    <catalog_category_view>
           <reference name="head">
                <action method="addJs"><script>yourscript.js</script></action>
            </reference>
    </catalog_category_view>

</layout>

Then in your modules config file, you will need something like:

<frontend>
    <layout>
        <updates>
           <yourmodule>
               <file>yourlayout.xml</file>
            </yourmodule>
         </updates>
     </layout>
 </frontend>

This assumes that yourscript.js is sitting in the root of the js folder. Obviously, you dont want to put it here so do what Jonathan advised and use:

 <action method="addItem"><type>skin_js</type><name>path/file.js</name></action>

and put your js in your themes skin folder.

Good luck!

Drew
A: 

Along those lines, does anyone know how to move the js script call to be at the end of the body? best practices say that any unnecessary js should be loaded at the end of the page, not in the head.

I understand that there's some Varien js code that needs to remain in the head, but for anything else...

dbcn
Scripts linked from the head might be merged using the experimental compression or any of several 'Minify' type extensions. This reduction in HTTP calls is potentially more effective than deferred execution. If you genuinely want to see suggestions then start a new question rather than hijacking others', I'm already thinking of answers.
clockworkgeek
@dbcn - yes, it is possible. Suggest you open a new question so that others can find your question and answer
Jonathan Day