PHP Magento Tips

How to add JS Programmatically in Magento?

Rate this posting:
{[['']]}

In Magento We can easily add JS With following ways.

Method 1: Via layout.xml with addJs method.

add below code in your layout.xml file.

<layout>
 <default>        
     <reference name="head">
       <action method="addJs" ifconfig="section/group/field">
          <script>yourjsfolder/jquery-3.1.1.min.js</script>
       </action>
     </reference>      
    </default>
</layout>


Method 2: Create Helper Function in layout.xml and include file accordingly.

add below code in your layout.xml file.

<layout>
 <default>        
     <reference name="head">
       <action method="addJs">
            <script helper="companyname_modulename/data/canShowjQuery" />
       </action>
     </reference>      
 </default>
</layout>

and inside your Helper/Data.php file add below code:

public function canShowjQuery()
    {
        if(Mage::getStoreConfig('mymodule/general/is_enabled') == true && Mage::getStoreConfig('mymodule/frontend/includejquery') == true) {
            return 'yourjsfolder/jquery-3.1.1.min.js';
        }
    }

You have to put your path Mage::getStoreConfig('mymodule/general/is_enabled') and Mage::getStoreConfig('mymodule/frontend/includejquery') from your system.xml accordingly

Method 3: Add JS file From your Block/Adminhtml

Open your app/code/community/companyname/modulename/Block/Adminhtml/entitycode/Edit.php file and create _prepareLayout function and add below code in it.

<?php
class companyname_modulename_Block_Adminhtml_entitycode_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
 protected function _prepareLayout() {
  if (Mage::registry('current_entitycode') && Mage::registry('current_entitycode')->getId()) {
   $type = Mage::registry('current_entitycode')->getType(); // Here i get my custom field value
   if ($type == 1){
    $this->getLayout()->getBlock('head')->addJs('jsfolder/custom1.js');
   } elseif ($type == 2){
    $this->getLayout()->getBlock('head')->addJs('jsfolder/custom2.js');
   } else {
    
   }
   
  } else {
   $this->getLayout()->getBlock('head')->addJs('jsfolder/custom.js');
  }
  
  return parent::_prepareLayout();
 }
}
?>

Here, i added custom1.js if my custom field type =1 , added custom2.js if my custom field type =2 and added custom.js if i didn't get any entity_id (means at add new record time).

Hope this post will help someone.
Thanks for reading....🙏🙏🙏🙏
Tag : Magento
2 Comments On "How to add JS Programmatically in Magento?"

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Typo3, kindly contact us http://www.maxmunus.com/contact
MaxMunus Offer World Class Virtual Instructor led training on Typo3. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email : arun@maxmunus.com
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –http://www.maxmunus.com


Thanks for the helpful post, it helped to many developers to use JS Programmatically in Magento.
https://www.facebook.com/AppJetty

Back To Top