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
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.
-
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.Klevis Miho– Klevis Miho2014年04月05日 15:47:00 +00:00Commented Apr 5, 2014 at 15:47
-
-
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.xmlKlevis Miho– Klevis Miho2014年04月05日 16:03:08 +00:00Commented Apr 5, 2014 at 16:03