{"id":287,"date":"2019-08-24T14:28:54","date_gmt":"2019-08-24T14:28:54","guid":{"rendered":"https:\/\/www.magetrend.com\/blog\/?p=287"},"modified":"2022-05-29T08:38:41","modified_gmt":"2022-05-29T08:38:41","slug":"how-to-add-custom-js-code-in-head-magento-2-beginners-guide","status":"publish","type":"post","link":"https:\/\/www.magetrend.com\/blog\/how-to-add-custom-js-code-in-head-magento-2-beginners-guide\/","title":{"rendered":"How to add custom js code in head &#8211; Magento 2 beginners guide"},"content":{"rendered":"\n<p>There are several different ways how to include custom javascript code into a head part of your page. But in this article I would like to cover the easiest one &#8211; the way which lets us to make it via Magento 2 admin side and doesn&#8217;t require programming skills or additional access. This method can be used to integrate 3rd party services, such as live support chat, some monitoring tool and etc.<\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_287&utm_campaign=pos1\"><img decoding=\"async\"  src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-pdf-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_287&utm_campaign=pos1\">The Most Popular PDF Designer for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_287&utm_campaign=pos1\">with user-friendly PDF editor to build your own PDF look<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-pdf-invoice?utm_source=article&utm_medium=post_287&utm_campaign=pos1\">Read More<\/a> <\/div> <\/div>\n\n\n\n<p>Bellow is the path of Magento 2 admin panel, where you can add your code snippet:<\/p>\n\n\n<div class=\"magento-path\"><span>Content &gt; Design &gt; Configuration &gt; Edit &gt; HTML Head &gt;  Scripts and Style Sheets<\/span><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Step by step guide for beginners<\/h2>\n\n\n\n<p>It seems simple, but for those, who aren&#8217;t very familiar with Magento, might cause some questions. So here is the completely, step by step, guide how to add your code snippet<\/p>\n\n\n\n<p>1. The first thing what we have to do is to go to admin panel and login. Usually Magento admin is hidden under \/admin.<\/p>\n\n\n\n<p>https:\/\/www.example.com\/<span class=\"highlight\">admin<\/span><\/p>\n\n\n\n<p>2. Then, in Magento menu, navigate to:<\/p>\n\n\n<div class=\"magento-path\"><span>Content &gt; Design &gt; Configuration<\/span><\/div>\n\n\n\n<p>3. Click &#8220;<strong>Edit<\/strong>&#8221; link on store view. Usually it will be the last one if your Magento has only one store view. If there are more, chose a website on which you need to include your script.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"811\" height=\"329\" src=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/06\/magento-2-change-logo-theme.jpg\" alt=\"\" class=\"wp-image-48\" srcset=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/06\/magento-2-change-logo-theme.jpg 811w, https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/06\/magento-2-change-logo-theme-300x122.jpg 300w, https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/06\/magento-2-change-logo-theme-768x312.jpg 768w\" sizes=\"(max-width: 811px) 100vw, 811px\" \/><\/figure>\n\n\n\n<p>4. Then click on the &#8220;<strong>HTML Head<\/strong>&#8221; tab.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"547\" height=\"335\" src=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/html-head-magento-2.jpg\" alt=\"\" class=\"wp-image-298\" srcset=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/html-head-magento-2.jpg 547w, https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/html-head-magento-2-300x184.jpg 300w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/figure><\/div>\n\n\n\n<p>5. Enter your code to &#8221;&nbsp;<strong>Scripts and Style Sheets&#8221;<\/strong> field and click on &#8220;Save Configuration&#8221; button<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"347\" src=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/hello-world-js-magento.jpg\" alt=\"\" class=\"wp-image-299\" srcset=\"https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/hello-world-js-magento.jpg 909w, https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/hello-world-js-magento-300x115.jpg 300w, https:\/\/www.magetrend.com\/blog\/wp-content\/uploads\/2019\/08\/hello-world-js-magento-768x293.jpg 768w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/figure><\/div>\n\n\n\n<p>If there are some other code already, your code should go after all other code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">A Code was added but no changes in store<\/h3>\n\n\n\n<ul><li><strong>Cache problem<\/strong> &#8211; Probably your page is cached and you need to clear it. You can clear your store cache here:<\/li><\/ul>\n\n\n<div class=\"magento-path\"><span>Magento Admin &gt; System &gt; Cache Management &gt; Select All &gt; Refresh &gt; Submit<\/span><\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>That&#8217;s all this time. If you had some other cases when it just didn&#8217;t work, let me know in the comments bellow. I will be more than happy to append this post with your case.<\/p>\n\n\n<div class=\"simple-container\"> <a class=\"simple-img\" href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_287&utm_campaign=pos2\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/prod\/magento-2-email-templates.png\" \/><\/a> <div class=\"simple-row simple-title\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_287&utm_campaign=pos2\">Responsive Emails Templates for Magento 2<\/a><\/div> <div class=\"simple-row simple-text\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_287&utm_campaign=pos2\">Our #1 bestseller. With user-friendly template editor!<\/a><\/div> <div class=\"simple-action\"><a href=\"https:\/\/www.magetrend.com\/magento-2-email-templates?utm_source=article&utm_medium=post_287&utm_campaign=pos2\">Read More<\/a> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are several different ways how to include custom javascript code into a head part of your page. But in this article I would like to cover the easiest one &hellip; <\/p>\n<div class=\"grid-footer\"><a href=\"https:\/\/www.magetrend.com\/blog\/how-to-add-custom-js-code-in-head-magento-2-beginners-guide\/\" class=\"more-link\">Read More <i class=\"mticon-arrow\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":324,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[21,22,20],"_links":{"self":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/287"}],"collection":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":20,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":475,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions\/475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media\/324"}],"wp:attachment":[{"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.magetrend.com\/blog\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}