2

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.

Thanks.

8
  • do you want to add a script in cms page on all pages?
    – Waqar Ali
    Commented 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
    Commented Jun 5, 2021 at 6:52
  • please see my answer
    – Waqar Ali
    Commented Jun 5, 2021 at 7:00
  • @Emily check this dckap.com/blog/exclude-script-from-magento-2-3 Commented 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
    Commented Jun 5, 2021 at 7:56

2 Answers 2

0

if you want to add this Script on you whole website

then go to

app/design/frontend/Vendor/Theme/Magento_Theme/layout/default_head_blocks.xml

at this code before <body> tag

<head>
     <link src="https://yourscriptlink.com/js_fileName.js"/>
</head>
3
  • 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
    Commented Jun 5, 2021 at 7:03
  • check my updated answer please
    – Waqar Ali
    Commented 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
    Commented Jun 5, 2021 at 7:15
0
  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 you have read our privacy policy.

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