Magento 1 – Load jQuery in Admin Panel Programmatically

adminadminhtmljavascriptmodule

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
    }
}

Best Answer

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();
   }

   [...]
}
Related Topic