Hi here am trying to add a js script to the <head> tag,
methods used
Content => Design => Configuration, Now click on the edit link of the current theme and Go to the HTML Head tab and inside scripts and style sheets text-area you can enter your custom script.
create an XML file on theme location:
app/design/frontend/packagename/themename/Magento_Theme/layout/default_head_blocks.xml
Created a template file on following location:
app/design/frontend/packagename/themename/Magento_Theme/templates/html/header/head_tags.phtml
<script type="text/javascript">
console.log("custom head!");
</script>
<script defer>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-NMWGPRJ');</script>
<!-- End Google Tag Manager -->
But it's not getting under Header, it's coming on Body.
where I'm doing wrong. can I get some help? Thank you in advance
-
Using method one by adding script via backend AND cleaning cache is working fine for me. Script is added to head-tag.nessi– nessi2024年04月30日 10:37:55 +00:00Commented Apr 30, 2024 at 10:37
1 Answer 1
Method 1: To add your Scripts and Style Sheets via admin panel:
- Go to CONTENT -> Design -> Configuration
- Click on the Edit link of your current theme.
- Under 'Other Settings' expand the
HTML Headsection. - Place your script under the 'Scripts and Style Sheets' input box.
- Click on the 'Save Configuration' button.
Method 2: To add your Scripts and Style Sheets via layout and template file in theme:
Go to location:
app/design/frontend/vendor/yourtheme/Magento_Theme/templates/html/head.phtmlAdd your script code in
head.phtmlfile.Save your file changes.
Clear your Magento store cache by running the below command after implementing the above changes.
php bin/magento cache:flush
Now check your script is placed under the <head> & </head> tags
-
hi @Ubed Shaikh Thank you for the response. Here I have used both methods. and added my script in
app/design/frontend/vendor/yourtheme/Magento_Theme/templates/html/head.phtmlandapp/design/frontend/vendor/yourtheme/Magento_Theme/templates/html/header.phtmlbut still script is running inside<body>.Praveen Negimani– Praveen Negimani2021年11月09日 15:57:21 +00:00Commented Nov 9, 2021 at 15:57 -
@PraveenNegimani Please use one method at a time. I recommend you for using Method 1 which is a default in Magento.IntraPersonalLearner– IntraPersonalLearner2021年11月10日 07:13:05 +00:00Commented Nov 10, 2021 at 7:13
-
I have the same problem. With method 1, which seems to be recommended, <head> starts on line 3. My script is at line 65... below many other scripts and fonts. The script is a cookie tool, which should load before all other stuff to function well. But I can't seem to get it to the top of the head. As for method 2: I searched for head.phtml in my whole magento 2 installation and found no file with that name (find /var/www/html/magento2 -type f -name "head.phtml") Is this normal and if I create it, it will work? In that case, do I only put my script tag in head.phtml and nothing more?Kai Mechel– Kai Mechel2023年05月25日 14:39:52 +00:00Commented May 25, 2023 at 14:39