Magento 2 – Add Custom Script After Body Tag

magento2magento2.3script

I am using Magento 2.3

Can anyone please provide appropriate way for

How can i add my custom script code after the tag.

Thanks

Best Answer

In your xml add below code,

<referenceContainer name="after.body.start">
    <block name="custom">
        <arguments>
            <argument name="text" xsi:type="string"><![CDATA[<script type="text/javascript" src="js/custom.js"></script>]]></argument>
        </arguments>
    </block>
</referenceContainer>

In case above code not works, you can do it by adding a phtml and declaring script in that phtml.

<referenceContainer name="before.body.end">
        <block template="Namespace_Modulename::custom.phtml" name="meaningful_name"/>
</referenceContainer>

In custom phtml, declare your script,

 <script type="text/javascript">Your script</script>
Related Topic