Conditionally load js based on cms static block type












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.










share|improve this question





























    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.










    share|improve this question



























      1












      1








      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.










      share|improve this question















      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.







      magento-1.9 module layout






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 27 '17 at 0:19







      Steve Karwacki

















      asked Jul 26 '17 at 23:49









      Steve KarwackiSteve Karwacki

      109110




      109110






















          1 Answer
          1






          active

          oldest

          votes


















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





          share|improve this answer





















          • 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.
            – Steve Karwacki
            Jul 27 '17 at 0:15











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f186679%2fconditionally-load-js-based-on-cms-static-block-type%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









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





          share|improve this answer





















          • 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.
            – Steve Karwacki
            Jul 27 '17 at 0:15
















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





          share|improve this answer





















          • 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.
            – Steve Karwacki
            Jul 27 '17 at 0:15














          0












          0








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





          share|improve this answer












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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 27 '17 at 0:08









          matthewninjamatthewninja

          1277




          1277












          • 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.
            – Steve Karwacki
            Jul 27 '17 at 0:15


















          • 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.
            – Steve Karwacki
            Jul 27 '17 at 0:15
















          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.
          – Steve Karwacki
          Jul 27 '17 at 0:15




          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.
          – Steve Karwacki
          Jul 27 '17 at 0:15


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f186679%2fconditionally-load-js-based-on-cms-static-block-type%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          An IMO inspired problem

          Management

          Has there ever been an instance of an active nuclear power plant within or near a war zone?