cancel
Showing results for 
Search instead for 
Did you mean: 

Loading Javascript file when module load

Loading Javascript file when module load

I am developing custom Module in Magento 1. Now I would like to load my module specific JS file when the module enabled. In this regard I used below xml code in a separate xml file named as module name which is placed inside layout folder. My module don't have any controller.

 

<default>
    <reference name="head">
        <action method="addJs">
            <js>brand_logo.js</js> 
        </action>
    </reference>
</default>

 

But the JS file is not loading at the time of module loading.

 

How can I do that??

6 REPLIES 6

Re: Loading Javascript file when module load

Hello foysal, 

 

You can check, if in your module configuration file -  here is the path:
 app/code/community/YOUR/MODULE/etc/config.xml

 

the layout file is set:

 

 

 

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

If you also need the script to connect only, if the module is enabled, perform the following:  add "ifconfig" parameter with the path to configuration option that enables/disables the module in your layout. Here is an example:

 

<default>
    <reference name="head">
        <action method="addJs" ifconfig="modulename/general/enabled">
            <script>filename.js</script>
        </action>
    </reference>
</default>

 

Regards,
Plumrocket Team

 

Re: Loading Javascript file when module load

Thanks @PlumrocketInc for your reply . I used your code like below but it is not working. 

<?xml version="1.0"?>
<layout version="0.1.0">
	<default>
		<reference name="head">
			<action method="addJs" ifconfig="brandlogo/general/enabled">
				<script>brand_logo.js</script>
			</action>
		</reference>
	</default>
</layout>



What should I write here  ??

ifconfig="brandlogo/general/enabled">

 Thanks

Re: Loading Javascript file when module load

Hi foysal,

 

In "ifconfig" - you need to specify the path to the option of your module, which responds for its enabling/disabling. 
This option should be created by yourself. System configurations are set in the following file:
 app/code/community/YOUR/MODULE/etc/system.xml
If you do not have such option, in this case you do not need to specify "ifconfig", as your script will not connect.

 

Regards,
Plumrocket Team

Re: Loading Javascript file when module load

Thanks @PlumrocketInc for your reply. Actually there is no such option (enabling/disabling) in my module. I am describing the scenario here. I can add a js file using page.xml file. In that case my js file will load each and every page, each and every condition. But I don't need that. I need only when my module is exist then my js file will load.

 

How can I achieve this target ??   

 

Thanks

Re: Loading Javascript file when module load

Um, if the JS is part of your module then it mean a priori that it only gets loaded when your module exists?

Tanel Raja

Re: Loading Javascript file when module load

Thanks @Pronto for your reply. But your words are not clear to me. Could you please explain a bit more?? 

 

Thanks