I want to include the js file on the header of the page. But I have selected the 'Move JS code to the bottom of the page' configuration from the admin panel. I want to include one specific script load on the header.

I have tried to include the Magento_Theme/default_head_blocks.xml file. Still, it's loading on the footer. How to add this file on the header. Any other option in code? Please let me know.


  • do you want to add a script in cms page on all pages?
    – Waqar Ali
    Jun 5, 2021 at 6:49
  • @WaqarAli yes. I have added a script for spam email. So it should be included in the header section.
    – Emily
    Jun 5, 2021 at 6:52
  • please see my answer
    – Waqar Ali
    Jun 5, 2021 at 7:00
  • @Emily check this dckap.com/blog/exclude-script-from-magento-2-3 Jun 5, 2021 at 7:51
  • @BilalUsean I have seen this. In default Magento, there is no option for this? If no, I will try the above link.
    – Emily
    Jun 5, 2021 at 7:56

2 Answers 2


if you want to add this Script on you whole website

then go to


at this code before <body> tag

     <link src="https://yourscriptlink.com/js_fileName.js"/>
  • I have tried the above method as well. But still, it's loading on the footer. Since But I have selected the 'Move JS code to the bottom of the page' configuration from the admin panel. Is there any other way to include jsfile on the head?
    – Emily
    Jun 5, 2021 at 7:03
  • check my updated answer please
    – Waqar Ali
    Jun 5, 2021 at 7:05
  • Please check my question. I have mentioned this. Tried with both the default.xml and dafault_head_blocks.xml files. But no luck.
    – Emily
    Jun 5, 2021 at 7:15
  1. Copy require_js.phtml from vendor/magento/module-theme/view/frontend/templates/page/js to your theme app/design/frontend/{VENDOR}/{THEME_NAME}/Magento_Theme/templates/page/js/

  2. Now you can add your script like this:

     console.log(""I'm loaded!"");
     var require = {
         ""baseUrl"": ""<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.