首页 > 解决方案 > drupal_add_html_head 就在 drupal_add_js 之后

问题描述

我想在外部添加 cookiebot 脚本

<script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" data-cbid="idcookiebot" data-culture="languageId" type="text/javascript"></script>

就在这之后

drupal_add_js("window.dataLayer = window.dataLayer || [];
    function gtag() { window.dataLayer.push(arguments); }
    gtag('consent', 'default', {
    ad_storage: 'denied',
    analytics_storage: 'denied',
    wait_for_update: 500,
    });",
    array('type' => 'inline')
  );

但是,使用 drupal_add_html_head,外部脚本位于第一行,但我需要在脚本之后使用它。

我找不到 drupal_add_js 如何使用数据元素添加外部 JS

标签: drupal

解决方案


您可以像这样设置scope额外weight的javascript:

drupal_add_js("window.dataLayer = window.dataLayer || [];
    function gtag() { window.dataLayer.push(arguments); }
    gtag('consent', 'default', {
    ad_storage: 'denied',
    analytics_storage: 'denied',
    wait_for_update: 500,
    });",
    array(
        'type' => 'inline',
        'scope' => 'footer',
        'weight' => 5,
    )
);

推荐阅读