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!');
-
Cleat all cache in admin end and try again. Also check you module enable or disabled under System -> Configuration -> Advanced under Advanced tabAbdul– Abdul2016年07月11日 05:50:22 +00:00Commented Jul 11, 2016 at 5:50
2 Answers 2
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
You can add your javascript file by adding a addJs method in page.xml file.
-
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.Hassan Khodadadeh– Hassan Khodadadeh2016年07月10日 11:27:12 +00:00Commented 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 jsLedian Hymetllari– Ledian Hymetllari2016年07月10日 12:08:21 +00:00Commented 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>Ledian Hymetllari– Ledian Hymetllari2016年07月11日 23:02:10 +00:00Commented Jul 11, 2016 at 23:02 -
OK I'll try that and will let you know what happens.Hassan Khodadadeh– Hassan Khodadadeh2016年07月12日 02:46:38 +00:00Commented Jul 12, 2016 at 2:46