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.
-
do you want to add a script in cms page on all pages?Waqar Ali– Waqar Ali2021年06月05日 06:49:43 +00:00Commented 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– Emily2021年06月05日 06:52:31 +00:00Commented Jun 5, 2021 at 6:52
-
please see my answerWaqar Ali– Waqar Ali2021年06月05日 07:00:24 +00:00Commented Jun 5, 2021 at 7:00
-
@Emily check this dckap.com/blog/exclude-script-from-magento-2-3Bilal Usean– Bilal Usean2021年06月05日 07:51:21 +00:00Commented 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– Emily2021年06月05日 07:56:24 +00:00Commented Jun 5, 2021 at 7:56
2 Answers 2
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>
-
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– Emily2021年06月05日 07:03:53 +00:00Commented Jun 5, 2021 at 7:03
-
check my updated answer pleaseWaqar Ali– Waqar Ali2021年06月05日 07:05:52 +00:00Commented 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– Emily2021年06月05日 07:15:07 +00:00Commented Jun 5, 2021 at 7:15
Copy require_js.phtml from
vendor/magento/module-theme/view/frontend/templates/page/jsto your themeapp/design/frontend/{VENDOR}/{THEME_NAME}/Magento_Theme/templates/page/js/Now you can add your script like this:
console.log(""I'm loaded!""); var require = { ""baseUrl"": ""<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>"" };