最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Add extra js in backend block (tab edit form) in Magento - Stack Overflow

matteradmin5PV0评论

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

Share Improve this question edited Nov 1, 2015 at 9:59 theB 6,7481 gold badge32 silver badges42 bronze badges asked Aug 16, 2012 at 12:01 WonderLandWonderLand 5,7047 gold badges59 silver badges77 bronze badges 3
  • possible duplicate of How to add JS programmatically in Magento? – benmarks Commented Aug 16, 2012 at 12:09
  • I think the problem is similar: ' While your code is successfully updating the head block class instance, it's happening after output has been generated from that instance' this sound as a confirm of what I feel is the problem. – WonderLand Commented Aug 16, 2012 at 12:20
  • I'm guessing if there is any code side solution – WonderLand Commented Aug 16, 2012 at 12:22
Add a ment  | 

2 Answers 2

Reset to default 4

Why not do this using layout XML? Ideally you would specify an adminhtml layout update file for your module, but you can add a local.xml file to the adminhtml/default/default/layout folder and in there do the normal <reference name="head"><action method="addJs">...</reference> call.

As suggested by @benmarks this is a particular situation but similar to the one described How to add JS programmatically in Magento?

The problem is the same 'head' block is already rendered.

I found a solution for this particular case ( add js in a edit tab form block in backend ):

to solve this I have added the above line in the _prepareLayout() method of the block that extends Mage_Adminhtml_Block_Widget_Form

Post a comment

comment list (0)

  1. No comments so far