Javascript 谷歌标签管理器-标签配置-追加<;脚本>;内容在<;脚本>;
我需要通过Google tag Manager中的自定义标记在元素前面包含两个标记:Javascript 谷歌标签管理器-标签配置-追加<;脚本>;内容在<;脚本>;,javascript,jquery,Javascript,Jquery,我需要通过Google tag Manager中的自定义标记在元素前面包含两个标记: 及 window.cookieconsent.initialise({“调色板”:{“弹出窗口”:{“背景”:“#000”},按钮:{“背景”:“#f1d600”}}) 因为我需要它们正好显示在元素之前,所以我使用jQuery通过appendTo()将这些值作为字符串追加: var cookieConsentStyles=''; var cookieConsentJS=''; var cookieConse
及
window.cookieconsent.initialise({“调色板”:{“弹出窗口”:{“背景”:“#000”},按钮:{“背景”:“#f1d600”}})代码>
因为我需要它们正好显示在元素之前,所以我使用jQuery通过appendTo()将这些值作为字符串追加:
var cookieConsentStyles='';
var cookieConsentJS='';
var cookieConsentInit='window.cookieconsent.initialise({“调色板”:{“弹出窗口”:{“背景”:“#000”},“按钮”:{“背景”:“#f1d600”}}});
jQuery(cookieConsentStyles).appendTo('head');
jQuery(cookieConsentJS).appendTo('body');
jQuery(cookieConsentInit).appendTo('body');
我已经在浏览器控制台中确认了这一点,但在谷歌标签管理器中不起作用,因为任何JS代码都需要在标签之间。因此,变量字符串中的标记即使是字符串变量的一部分,也会得到解释。请参见下面的屏幕截图
我尝试使用适当的实体(即>;)来逃避小于和大于符号,但也失败了 将Ibu的答案标记为最终答案,因为他使用onload的示例为我提供了一种我不知道的替代方法(直接运行window.cookieconsent.initialise,但由于某些原因无效)
根据他的例子,精确的解决方案如下:
var cookieConsentJS = document.createElement( 'script' );
cookieConsentJS.type = 'text/javascript';
cookieConsentJS.src = 'https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js';
cookieConsentJS.setAttribute('data-cfasync', false);
var cookieConsentInit = document.createElement( 'script' );
cookieConsentInit.innerText = 'window.cookieconsent.initialise({"palette":{"popup":{"background":"#000"},"button":{"background":"#f1d600"}}});';
cookieConsentJS.onload = function() {
document.body.appendChild( cookieConsentInit );
};
document.body.appendChild( cookieConsentJS );
使用onload
事件加载父脚本时,可以运行cookie同意脚本。以下是一个例子:
var cookieConsentJS = document.createElement( 'script' );
cookieConsentJS.type = 'text/javascript';
cookieConsentJS.src = '/path/to/cookieconsent.min.js';
cookieConsentJS.setAttribute('data-cfasync', false); // not sure what this is, but not touching.
// this part of the code will run when the script is loaded.
cookieConsentJS.onload = function() {
// here is your code running outside of a string.
window.cookieconsent.initialise({
"palette":{
"popup":{"background":"#000"},
"button":{"background":"#f1d600"}
}
});
};
// now let's append your code to the body.
document.body.appendChild( cookieConsentJS );
请注意,jQuery没有在这个上下文中使用。我建议不要在页面中添加cookieConsentInit
。如果它在第一步工作,这意味着它与在代码中运行eval相同。如果任何第三方可以访问您的变量,他们可以在您的页面上附加恶意代码。为什么不直接运行代码?您可以直接运行代码,而不是为cookieConsentInit
创建脚本标记。无需将其转换为字符串,然后在可以自己运行js时追加它。另外,它更安全。@Ibu在Google Tag Manager的上下文中,如果不通过jQuery选择该标记并通过字符串添加代码,如何确保代码直接在结束正文标记之前运行?出于组织上的原因,我需要使用谷歌标签管理器。
var cookieConsentJS = document.createElement( 'script' );
cookieConsentJS.type = 'text/javascript';
cookieConsentJS.src = '/path/to/cookieconsent.min.js';
cookieConsentJS.setAttribute('data-cfasync', false); // not sure what this is, but not touching.
// this part of the code will run when the script is loaded.
cookieConsentJS.onload = function() {
// here is your code running outside of a string.
window.cookieconsent.initialise({
"palette":{
"popup":{"background":"#000"},
"button":{"background":"#f1d600"}
}
});
};
// now let's append your code to the body.
document.body.appendChild( cookieConsentJS );