我正在尝试向我的页面动态添加 Floodlight 代码。该页面被多家不同的公司使用,因此我们实现了动态添加跟踪和分析,以便每个公司都可以添加自己的跟踪。
我有一个div:<div id="bodyTrackingCode"></div>
它位于正文开始标签的正下方。
我使用 .html 插入跟踪代码:$('#bodyTrackingCode').html(Info.BodyTracking);
这适用于所有类型的 Google 分析代码。不幸的是,我的 Floodlight 代码有问题。我相信这是因为代码中的“document.write”。
代码如下所示:
<!-- Start of DoubleClick Floodlight Tag: Please do not remove Activity name of this tag: xxxxxx to Call-Landing URL of the webpage where the tag is expected to be placed: xxxxxxx This tag must be placed between the <body> and </body> tags, as close as possible to the opening tag. Creation Date: 05/01/2018 --> <script type="text/javascript"> var axel = Math.random() + ""; var a = axel * 10000000000000; document.write('<iframe src="https://3948978.fls.doubleclick.net/activityi;src=xxxxxxx;type=hea-l0;cat=jj-he0;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;ord=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>'); </script> <noscript> <iframe src="https://3948978.fls.doubleclick.net/activityi;src=xxxxxxx;type=hea-l0;cat=jj-he0;dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;ord=1?" width="1" height="1" frameborder="0" style="display:none"></iframe> </noscript> <!-- End of DoubleClick Floodlight Tag: Please do not remove -->
我尝试转义结束 iframe 标记和结束脚本标记。它给了我一个语法错误意外标记的控制台错误 <
当我转义标签时,页面会加载。如果我不转义标签,页面就会变成一个大白屏。我怀疑 document.write 正在替换正文代码。
我不知道如何让它正常工作。救命!!!
最佳答案
如果您无法控制 document.write,则可以在加载脚本之前插入此内容来执行此操作:
var myWrite = document.write, myWriteLN=document.writeln, dbhtml;
document.write=function(str) {
dbhtml += str;
}
$(function() {
$('#bodyTrackingCode').html(dbhtml);
});
关于javascript - 如何动态添加 Floodlight 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50396732/