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
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIn 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>
Step 1. Create a custom file custom_body.phtml to add your script code right after body tag.
custom_body.phtml
/app/design/frontend/Vendor/YourTheme/Magento_Theme/templates/html/custom_body.phtml
Write you script code :
<script type="text/javascript">Your script</script>
Step 2.
default .xml file: /app/design/frontend/Vendor/YourTheme/Magento_Theme/layout/default.xml
add this:
<referenceContainer name="after.body.start">
<block name="custom.body" before="-" template="Magento_Theme::html/custom_body.phtml" />
</referenceContainer>
I know this is an old question but what worked for me was adding class="Magento\Framework\View\Element\Text" to aravind's answer