- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want to add custom script just after the start of head tag. I want to do that because if i add my customer script using
like.
<head>
<script>console.log("I'm loaded!");</script>
i tried to add code in default_head_blocks.xml
<referenceContainer name="head.additional"> <block class="Custom\Module\Block\Success" template="Custom_Module::success/head.phtml"/> </referenceContainer> Output: <script>console.log("I'm loaded!");</script> </head>
Header.additional add script before the end of head tag while i want to add the script just after the starting <head> tag.
I want to do that because I am using third part Js which is generating Mismatch error with require.js did follow the steps to prevent the mismatch but no solution.
So we did a trick to load that script before the require.js and just after the starting <head> tag.
Any help would be appreciated.
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks rakesh but i did try that as well but it adds it after the require.js file. Reason why i want to load my custom third party script code before require.js because it's giving mismatch error.
Did try the solutions mentioned by require.js which doesn't work as well so though to add it before require.js call which worked fine.
Solution someone explained here worked for me; https://stackoverflow.com/questions/48167553/magento-2-add-custom-script-just-after-head-tag
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add custom script just after starting <head> tag before Require.js script
You need to use script tag with below way like,
<script src="jsfoler/jsfile.js"/>
you cant directly call script with console in your xml file.
Magento 2 Blogs/Tutorial
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks rakesh but i did try that as well but it adds it after the require.js file. Reason why i want to load my custom third party script code before require.js because it's giving mismatch error.
Did try the solutions mentioned by require.js which doesn't work as well so though to add it before require.js call which worked fine.
Solution someone explained here worked for me; https://stackoverflow.com/questions/48167553/magento-2-add-custom-script-just-after-head-tag
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add custom script just after starting <head> tag before Require.js script
I need to google tag manager js under the head tag but i enabled "Move JS code to the bottom of the page". I need just one js on the top under the head tag.
Is it possible?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add custom script just after starting <head> tag before Require.js script
HI @mital_shah ,
I need also to have the Google Tag Manager tag just under the head tag.
Have you found a solution for this please ?
Regards,