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.

asked Jun 5, 2021 at 6:28
8
  • do you want to add a script in cms page on all pages? 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. Commented Jun 5, 2021 at 6:52
  • please see my answer 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. 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>
answered Jun 5, 2021 at 7:00
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? Commented Jun 5, 2021 at 7:03
  • check my updated answer please 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. 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('/') ?>""
     };
    
answered Jun 10, 2021 at 2:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.