{"id":57278,"date":"2023-05-26T22:16:08","date_gmt":"2023-05-26T16:46:08","guid":{"rendered":"https:\/\/2thenew.today\/blog\/?p=57278"},"modified":"2023-05-30T09:31:03","modified_gmt":"2023-05-30T04:01:03","slug":"add-inline-javascript-code-in-drupal-9","status":"publish","type":"post","link":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/","title":{"rendered":"Add inline Javascript code in Drupal 9"},"content":{"rendered":"<p>In our Drupal projects, we have requirements to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements. So in the blog, we will see how we can achieve this by both methods.<\/p>\n<p>Drupal provides hooks to do this in a proper way. To add the inline javascript code in the page header, we can use <a href=\"\/\/api.drupal.org\/api\/drupal\/core%21lib%21Drupal%21Core%21Render%21theme.api.php\/function\/hook_page_attachments\/8.2.x\u201d\">hook_page_attachments()<\/a>, and for the page bottom, we can use hook <a href=\"\/\/api.drupal.org\/api\/drupal\/core%21lib%21Drupal%21Core%21Render%21theme.api.php\/function\/hook_page_bottom\/8.2.x\u201d\">hook_page_bottom() <\/a><\/p>\n<p>Let\u2019s understand this with examples.<\/p>\n<p><strong>Method 1: Add inline Javascript code in page header<\/strong><\/p>\n<pre>\/**\r\n* Implements hook_page_attachments().\r\n*\/\r\nfunction\u00a0<strong>MYMODULE_page_attachments<\/strong>(array &amp;$attachments)\u00a0{\r\n\/\/ You can optionally perform a check to make sure you target a specific page.\r\nif\u00a0(\\Drupal::routeMatch()-&gt;getRouteName() ==\u00a0'some.route') {\r\n\/\/ Add our JS.\r\n$attachments['#attached']['html_head'][] = [\r\n[\r\n'#tag'\u00a0=&gt;\u00a0'script',\r\n'#attributes'\u00a0=&gt; [\r\n'type'\u00a0=&gt;\u00a0'text\/javascript',\r\n],\r\n'#value'\u00a0=&gt; \\Drupal\\Core\\Render\\Markup::create('console.log(\u201cThis\u201d is my javascript code);\u2019),\r\n],\u00a0\u00a0'key_for_this_snippet',\r\n];\r\n}\r\n}<\/pre>\n<p><strong>Method 2: Add inline Javascript code in page bottom<\/strong><\/p>\n<pre>\/**\r\n* Implements hook_page_bottom().\r\n*\/\r\nfunction <strong>MYMODULE_page_bottom<\/strong>(array &amp;$page_bottom) {\r\n$user = \\Drupal::currentUser();\r\n$page_bottom['pixelthis_sticky_banner'] = [\r\n'#markup' =&gt; \\Drupal\\Core\\Render\\Markup::create('console.log(\u201cThis\u201d is my javascript code);\u2019),\r\n'#cache' =&gt; [\r\n'contexts' =&gt; ['user'],\r\n'tags' =&gt; ['user:' . $user-&gt;id()],\r\n],\r\n];\r\n}<\/pre>\n<p>You can add this JS script for global or page specific as per your requirements. For that, add conditions before rendering the markup. Also, we can add the same by using route and controller.<\/p>\n<p>If you still have questions, comment and join the discussion. I will be adding different ways in the upcoming blogs. Stay tuned.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In our Drupal projects, we have requirements to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements. So in the blog, we will see how we can achieve this by both methods. Drupal provides hooks to do this in a [&hellip;]<\/p>\n","protected":false},"author":1508,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":127,"footnotes":""},"categories":[3602],"tags":[4884,5212],"class_list":["post-57278","post","type-post","status-publish","format-standard","hentry","category-drupal","tag-drupal-9","tag-inline-javascript"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Priya Roy Chowdhury\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Add inline Javascript code in Drupal 9 | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Add inline Javascript code in Drupal 9 | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#article\",\"name\":\"Add inline Javascript code in Drupal 9 | TO THE NEW Blog\",\"headline\":\"Add inline Javascript code in Drupal 9\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/priya-chowdhury\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2023-05-26T22:16:08+05:30\",\"dateModified\":\"2023-05-30T09:31:03+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#webpage\"},\"articleSection\":\"Drupal, Drupal 9, inline Javascript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/drupal\\\/#listItem\",\"name\":\"Drupal\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/drupal\\\/#listItem\",\"position\":2,\"name\":\"Drupal\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/drupal\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#listItem\",\"name\":\"Add inline Javascript code in Drupal 9\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#listItem\",\"position\":3,\"name\":\"Add inline Javascript code in Drupal 9\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/drupal\\\/#listItem\",\"name\":\"Drupal\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/priya-chowdhury\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/priya-chowdhury\\\/\",\"name\":\"Priya Roy Chowdhury\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/5c8b4c4b-7679-4ef1-a872-2818902cf076_4601-Priya-Roy-Chowdhury-PROFILEPICTURE.jpeg\",\"width\":96,\"height\":96,\"caption\":\"Priya Roy Chowdhury\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/\",\"name\":\"Add inline Javascript code in Drupal 9 | TO THE NEW Blog\",\"description\":\"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/add-inline-javascript-code-in-drupal-9\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/priya-chowdhury\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/priya-chowdhury\\\/#author\"},\"datePublished\":\"2023-05-26T22:16:08+05:30\",\"dateModified\":\"2023-05-30T09:31:03+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Add inline Javascript code in Drupal 9 | TO THE NEW Blog","description":"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.","canonical_url":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#article","name":"Add inline Javascript code in Drupal 9 | TO THE NEW Blog","headline":"Add inline Javascript code in Drupal 9","author":{"@id":"https:\/\/2thenew.today\/blog\/author\/priya-chowdhury\/#author"},"publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"},"datePublished":"2023-05-26T22:16:08+05:30","dateModified":"2023-05-30T09:31:03+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#webpage"},"articleSection":"Drupal, Drupal 9, inline Javascript"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.today\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/drupal\/#listItem","name":"Drupal"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/drupal\/#listItem","position":2,"name":"Drupal","item":"https:\/\/2thenew.today\/blog\/category\/drupal\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#listItem","name":"Add inline Javascript code in Drupal 9"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#listItem","position":3,"name":"Add inline Javascript code in Drupal 9","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.today\/blog\/category\/drupal\/#listItem","name":"Drupal"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.today\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.today\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.today\/blog\/author\/priya-chowdhury\/#author","url":"https:\/\/2thenew.today\/blog\/author\/priya-chowdhury\/","name":"Priya Roy Chowdhury","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/5c8b4c4b-7679-4ef1-a872-2818902cf076_4601-Priya-Roy-Chowdhury-PROFILEPICTURE.jpeg","width":96,"height":96,"caption":"Priya Roy Chowdhury"}},{"@type":"WebPage","@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#webpage","url":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/","name":"Add inline Javascript code in Drupal 9 | TO THE NEW Blog","description":"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.today\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.today\/blog\/author\/priya-chowdhury\/#author"},"creator":{"@id":"https:\/\/2thenew.today\/blog\/author\/priya-chowdhury\/#author"},"datePublished":"2023-05-26T22:16:08+05:30","dateModified":"2023-05-30T09:31:03+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.today\/blog\/#website","url":"https:\/\/2thenew.today\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.today\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Add inline Javascript code in Drupal 9 | TO THE NEW Blog","og:description":"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.","og:url":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/","og:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Add inline Javascript code in Drupal 9 | TO THE NEW Blog","twitter:description":"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements.","twitter:image":"https:\/\/2thenew.today\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"57278","title":null,"description":"Drupal provides hooks to add some kind of inline javascript code, like adding GTM script. This can be in the page header or page bottom as per requirements. &nbsp;","keywords":[],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"#aioseo-article-65e05023205be","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"limit_modified_date":false,"created":"2023-05-10 10:05:39","updated":"2024-02-29 09:36:35","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.today\/blog\/category\/drupal\/\" title=\"Drupal\">Drupal<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tAdd inline Javascript code in Drupal 9\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.today\/blog"},{"label":"Drupal","link":"https:\/\/2thenew.today\/blog\/category\/drupal\/"},{"label":"Add inline Javascript code in Drupal 9","link":"https:\/\/2thenew.today\/blog\/add-inline-javascript-code-in-drupal-9\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/57278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/users\/1508"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/comments?post=57278"}],"version-history":[{"count":5,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/57278\/revisions"}],"predecessor-version":[{"id":57354,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/posts\/57278\/revisions\/57354"}],"wp:attachment":[{"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/media?parent=57278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/categories?post=57278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.today\/blog\/wp-json\/wp\/v2\/tags?post=57278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}