cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom script just after starting <head> tag before Require.js script

SOLVED

Add custom script just after starting <head> tag before Require.js script

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.

2 REPLIES

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.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Add custom script just after starting <head> tag before Require.js script

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

Tags (2)