1

Let's assume I have a company named Company and I'm developing a magento module named Module. What I want my module to do is simple: Add a JavaScript file to header tag of ALL pages in magento. But for some reason, it doesn't add the script in any pages.

Note: Currently I'm testing it for frontend.

Note 2::I'm using XAMPP (php 5.6, mysql 5.6, apache 2) if that's relevant.

Here is my directory tree:

Root
├───app
│   ├───code
│   │   └───local
│   │       └───Company
│   │           └───Module
│   │               └───etc
│   │                       config.xml
│   │
│   ├───design
│   │   └───frontend
│   │       └───base
│   │           └───default
│   │               └───layout
│   │                       module.xml
│   │
│   └───etc
│       └───modules
│               Company_Module.xml
│
└───js
    └───company
            module.js

Content of app\etc\modules\Company_Module.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Company_Module>
    </modules>
</config>

Content of app\code\local\Company\Module\etc\config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <version>0.1.0</version>   
        </Company_Module>
    </modules>
    <frontend>
        <layout>
            <updates>
                <module>
                    <file>module.xml</file> 
                </module>
            </updates>
        </layout>
    </frontend>
</config>

Content of app\design\frontend\base\default\layout\module.xml:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="head">
            <action method="addJs">
                <script>company/module.js</script>
            </action>
        </reference>
    </default>
</layout>

Content of js\company\module.js :

alert('Yes!');
1
  • Cleat all cache in admin end and try again. Also check you module enable or disabled under System -> Configuration -> Advanced under Advanced tab
    – Abdul
    Commented Jul 11, 2016 at 5:50

2 Answers 2

4

Contents of app/code/local/Company/Module/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <name>Company </name>
            <version>0.0.1</version>
        </Company_Module>
    </modules>
    <frontend>
        <layout>
            <updates>
                <module>
                    <file>company_module.xml</file>
                </module>
            </updates>
        </layout>
    </frontend>
 <adminhtml>
        <layout>
            <updates>
                <module>
                    <file>company_module.xml</file>
                </module>
            </updates>
        </layout>
</adminhtml>
</config>

Contents of app/design/frontend/base/default/layout/company_module.xml:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="head">
            <action  method="addItem"><type>js</type><name>company/module/javascript.js</name></action>
        </reference>
    </default>
</layout>

Path to JS file: js/company/module/javascript.js

0

You can add your javascript file by adding a addJs method in page.xml file.

4
  • I can't edit that file. I need a way to add javascript without editing core files. I have to give this module to my customers, so adding or editing core files is NOT an option. I'll look for a way to override or extend that file. Thanks.
    – undone
    Commented Jul 10, 2016 at 11:27
  • I thought to copy page.xml to your theme. You can add javascript to your layout file of your module. Reference to your root xml block then add js Commented Jul 10, 2016 at 12:08
  • Add the JS file somewhere into js folder and paste the following code to your XML layout file on your module <default> <reference name="head"> <action method="addJs"><script>folder/file.js</script></action> </reference> </default> Commented Jul 11, 2016 at 23:02
  • OK I'll try that and will let you know what happens.
    – undone
    Commented Jul 12, 2016 at 2:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.