0

I successfully added some custom JS and CSS to my head section (look here).

But If I try to add a script at the block before_body_end then I get:

There has been an error processing your request

Invalid method Mage_Core_Block_Text_List::addItem(Array
(
    [0] => skin_js
    [1] => black/NewsletterPopup/js/main.js
)

app\design\frontend\base\default\layout\black\newsletter_popup.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="head">
            <action method="addItem">
                <type>skin_js</type>
                <script>black/NewsletterPopup/js/jquery.magnific-popup.min.js</script>
            </action>
            <action method="addCss">
                <stylesheet>black/NewsletterPopup/css/magnific-popup.css</stylesheet>
            </action>
            <action method="addCss">
                <stylesheet>black/NewsletterPopup/css/main.css</stylesheet>
            </action>
        </reference>

        <reference name="before_body_end">
            <action method="addItem">
                <type>skin_js</type>
                <script>black/NewsletterPopup/js/main.js</script>
            </action>
        </reference>
    </default>
<!-- ... -->
4
  • 1
    because addItem method not exist core/text_list you can call your custom modules block there.
    – kunj
    Commented Jun 4, 2018 at 13:36
  • Ok I see, addItem is not supported for blocks with type core/text_list for whatever reason. I can call my modules block which includes html., But how can I properly include my script?
    – Black
    Commented Jun 4, 2018 at 13:57
  • 1
    your way is proper to include JS file. you can access js functions in your block which you added under before_body_end.
    – kunj
    Commented Jun 4, 2018 at 14:01
  • Ok I see, so I could just add a <script></script> block to the template with the script.
    – Black
    Commented Jun 4, 2018 at 14:03

1 Answer 1

1

I solved it by changing my layout file to:

<layout>
    <!-- ... --->
    <default>
        <!-- ... --->
        <reference name="before_body_end">
            <block type="core/text" name="newsletterpopup_main.js">
                <action method="setText">
                    <text><![CDATA[<script type="text/javascript" src="skin/frontend/base/default/fekete/NewsletterPopup/js/main.js"></script>]]></text>
                </action>
            </block>
        </reference>
        <!-- ... --->
    </default>
    <!-- ... --->
</layout>

I found the answer here.

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