0

I am creating a module and right now I have created only the config.xml:

<config>
    <modules>   
        <Namespace_moduleName>      
            <version>1.1.0</version>
        </Namespace_moduleName>
    </modules>
</config>

And Namespace_modulename.xml:

<config>
    <modules>
        <Namespace_moduleName>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_moduleName>
    </modules>
</config>

What is the best practice to include a javascript file? Also where should I put the javascript?

1 Answer 1

2

You can put the js files in the js folder or in the skin folder inside a theme. (base/default to make it available for all the themes.).
I would go with the js folder.

And to include it in a page add this to the config.xml file of the module.

<frontend>
    <layout>
        <updates>
            <namespace_modulename>
                <file>namespace_modulename.xml</file>
            </namespace_modulename>
        </updates>
    </layout>
</frontend>

This will tell Magento that your module has a layout file. (namespace_modulename.xml).

Now create the layout file. app/design/frontend/base/default/layout/namespace_modulename.xml with this content.

<xml version="1.0"?>
<layout>
    <default>
        <reference name="head">
            <action method="addJs"><js>path/to/js/file.js</js></action> <!-- path must be relative to the `js` folder -->
        </reference>
    </default>
</layout>

This will add the js file to all the pages.

3
  • I added the <frontend>... inside the <config> of my config.xml and the namespace_modulename.xml to app/design/frontend/default/mytheme/layout/ but it doesn't work. Commented Apr 5, 2014 at 15:47
  • clear the cache. Commented Apr 5, 2014 at 15:58
  • It is disabled. What I have now is (only those files): app/etc/modules/namespace_modulename.xml app/code/local/namespace/modulename/etc/config.xml app/design/frontend/default/mytheme/layout/namespace_modulename.xml Commented Apr 5, 2014 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.