1

What I want to achieve: I want a local js file included in the <head> only when a particular static block type is called.

Example: When "gallery/view" block type is called like so, {{block type="gallery/view" name="catalog.gallery" template="catalog/category/category-gallery.phtml"}}, I want gallery.js to load.

The issue: I believe the layout xml has already been processed before magento renders static blocks. So I don't think I can add to the <head> from within the Namespace_Gallery_Block_View class.

Obviously I could just add gallery.js via local.xml on every page but the entire point of this is to limit filesize and only call the resources that are absolutely needed per page.

Additionally, I want to use js combination so i have to actually update the layout somehow.

Does anyone have an alternate method or idea to get this working in this manner? I'm not sure if what I want do is actually possible.

asked Jul 26, 2017 at 23:49

1 Answer 1

0

Add this somewhere in category-gallery.phtml

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'url'; 
document.getElementsByTagName('head')[0].appendChild(script);
answered Jul 27, 2017 at 0:08
1
  • Sorry, should have been more specific. I want to use magento's js combination feature so the js file has to register with the layout system in some way well before php is done executing. Commented Jul 27, 2017 at 0:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.