0

谢谢你看这个。我认为我的想法犯了一个概念性错误,这就是为什么我会先让你知道我的情况:

我有 1 个或 x 个 DIV,在其中显示 DFP AdUnit,并将使用这些动态生成的函数。只要 DIV 位于可见区域,该函数就会触发:

  1. 我动态生成链接

  function scriptincluder(divid){
    var gbgscript = document.createElement('script');
    gbgscript.async = true;
    gbgscript.type = 'text/javascript';
    gbgscript.src = 'https://anyscript.net/script.js?function=myfmydiv1&div=mydiv1 ';
    var node = document.getElementsByTagName('script')[0];
    node.parentNode.insertBefore(gbgscript, node);
  }

使用此功能,我可以动态创建链接,并且到目前为止有效。所以我为 myfmydiv1/div1、myfmydiv2/div2、myfmydiv3/div3……生成链接。并将它们添加到父节点。

  1. 我动态生成 AdSlot

 googletag.cmd.push(function() {
        for (var slot in divslots) {
                window['slot_'.concat(slot.toString())] = googletag.defineSlot('/Adslot/Adslot/Adslot/Adslot/Adslot/Adslot', slotsize[slot], slot.toString()).addService(googletag.pubads());

                // generate external link pixel from #1:
                scriptincluder(slot.toString());
        }
        googletag.pubads().enableSingleRequest();
        googletag.pubads().disableInitialLoad();  // ad unit will not render yet
        googletag.enableServices();
    });

在这一部分中,我生成广告单元并将其添加到全局变量“window['slot_'.concat(slot.toString())]”(<== 我在网上看到过这个,我很好奇这是不是正确的方法。至少我可以在 GCR 开发工具中看到它)

  1. 我动态生成引用#1 处链接的函数。

  for (var slot in divslots) {
    var [‘myf’ + escape(slot)] = function() { 
        alert("I am: " + slot);
        googletag.cmd.push(function() {
            googletag.pubads().refresh([window['slot_'.concat(key2.toString())]]);});
    }
  }

一旦 DIV 插槽位于可见区域并刷新广告单元,就会触发该功能。

它总是触发错误的功能。比如div1触发了div2的函数,div1实际上并没有加载,而是div2。有什么想法/帮助吗?

4

1 回答 1

0

我与一位经验丰富的程序员同事一起找到了解决方案。他建议在最后一段代码中使用cont 变量作为变量函数。

  for (var slot in divslots) {
    
    const myFunction = 'myf' + escape(slot);
    const mySlot = 'slot_'.concat(slot.toString());
  
    var [myFunction] = function() { 
        alert("I am: " + slot);
        googletag.cmd.push(function() {
            googletag.pubads().refresh([window[mySlot]]);});
    }
  }

于 2017-12-19T17:07:41.037 回答