1

I want to load jQuery in admin panel. How can I do this programmatically without creating layout XML files in /app/design/adminhtml/default/default/layout/? Method with layout XML is described in this thread: https://stackoverflow.com/questions/12799838/how-to-load-a-js-file-on-magento-admin-dashboard

Is there any way to do this programmatically in my custom module? The module adds some new form fields which can be used on admin pages under System->Configuration. And there I need jQuery to be loaded.

EDIT

In my module I have this custom field in which I need to use jQuery:

class Company_Helloworld_Block_Adminhtml_Mybutton extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        //...here add some custom button functionality
    }
}
1

You can access the layout programmatically via the getLayout method. Your block class would look something like this

class [Namespace]_[Module]_Block_[Blockname]
{
   public function _prepareLayout() 
   {
      $head = $this->getLayout()->getBlock('head');
      $head->addJs('js/jquery.latest.js');

      return parent::_prepareLayout();
   }

   [...]
}
  • Thank you very much, but I'm not sure how to apply this to my module. I edited my question. I would appreciate if you could show my how to implement your sample to my code. – d3tr5845 May 12 '14 at 10:18
  • You can add the _prepareLayout method to that block class – Sander Mangel May 12 '14 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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