title: Magento 2 : Add custom script just after head tag
body: <p>i want to add custom script just after the start of head tag.</p>
  
  <p>like.</p>
  
  <pre><code>&lt;head&gt;
  &lt;script&gt;console.log("I'm loaded!");&lt;/script&gt;
  </code></pre>
  
  <p>i tried to add code in default_head_blocks.xml</p>
  
  <pre><code>&lt;referenceContainer name="head.additional"&gt;
        &lt;block class="Custom\Module\Block\Success" template="Custom_Module::success/head.phtml"/&gt;
  &lt;/referenceContainer&gt;
  </code></pre>
  
  <p>=> output :</p>
  
  <pre><code>&lt;script&gt;console.log("I'm loaded!");&lt;/script&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p>this code are using add script before the end of head tag.</p>
  
  <p>Please check Below code</p>
  
  <p><strong>Block => Custom/Module/Block/Onepage/Success.php</strong></p>
  
  <pre><code>namespace Custom\Module\Block\Onepage;
      use Magento\Framework\View\Element\Template;
  
      class Success extends \Magento\Checkout\Block\Onepage\Success {
  
      public function getOrder() 
          {
              $objectManager =\Magento\Framework\App\ObjectManager::getInstance();
              $helper = $objectManager-&gt;get('Custom\Module\Helper\Data');
  
              $lastOrderId = $this-&gt;getOrderId();
  
              if (empty($lastOrderId)) 
              {
                  return null;
              }
                $orderData = $objectManager-&gt;create('Magento\Sales\Model\Order')-&gt;loadByIncrementId($this-&gt;getOrderId());
  
              return $orderData;
          }
  
      }
  </code></pre>
  
  <p><strong>Helper => Custom\Module\Helper\Data.php</strong>
  
  
  <pre><code>namespace Custom\Module\Helper;
  class Data extends \Magento\Framework\App\Helper\AbstractHelper
  {
  
      /**
       * @param \Magento\Framework\App\Helper\Context $context
       */
      protected $_request;
  
      public function __construct(
          \Magento\Framework\App\Helper\Context $context,
          \Magento\Framework\App\Request\Http $request
      ) {
           $this-&gt;_request = $request;
          parent::__construct($context);
  
      }
       public function getConfigValue($value = '') {
          return $this-&gt;scopeConfig
                  -&gt;getValue($value,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
      }
      public function getTemplate()
      {
          if ($this-&gt;getConfigValue('custom_general/general/active') == 1) {
              $template =  'Custom_Module::checkout/success.phtml';
          } else {
              $template = 'Magento_Checkout::success.phtml';
          }
  
          return $template;
      }
  }
  </code></pre>
  
  <p><strong>di.xml => etc\di.xml</strong></p>
  
  <pre><code>&lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd"&gt;
      &lt;preference for="Magento\Checkout\Block\Onepage\Success" type="Custom\Module\Block\Onepage\Success"/&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p><strong>Layout Xml => Custom/Module/view/frontend/layout/default.xml</strong></p>
  
  <pre><code>&lt;page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"&gt;
      &lt;body&gt;
      &lt;referenceBlock name="require.js"&gt;
          &lt;action method="setTemplate"&gt;
              &lt;argument name="template" xsi:type="string"&gt;Custom_Module::success/head.phtml&lt;/argument&gt;
          &lt;/action&gt;
      &lt;/referenceBlock&gt; 
      &lt;/body&gt;
  &lt;/page&gt;
  </code></pre>
  
  <p><strong>Template => Custom/Module/view/frontend/templates/success/head.phtml</strong></p>
  
  <pre><code>&lt;script&gt;
      console.log("I'm loaded!");
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Please help me and solve this issue</p>
  
  <p>Thanks in advance.</p>