0

我的 JS 将作为通过 iframe 的第 3 方代码包含在主机页面中。当它加载时,我想提供一个新的 div 容器(“myDiv”)并访问首页上预先存在的 Google Ad Manager 实例(它已经为引用“myDiv”定义了一个槽)并使用它来调用googletag.display('myDiv')在我在 iframe 内交付的 div 内。

<!-- Top Host Page Source -->
<html>
    <head>
        <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
        <script>
            var googletag = googletag || {};
            googletag.cmd = googletag.cmd || [];

            googletag.cmd.push(function() {
                googletag.defineSlot('/0123456789/Something', [320, 50], 'someOtherDiv').addService(googletag.pubads());
                googletag.defineSlot('/0123456789/Something/else', [320, 50], 'myDiv').addService(googletag.pubads());
                googletag.pubads().enableSingleRequest();
                googletag.enableServices();
            });
        </script>
    </head>
    <body>
        <h1>My Host Top Page</h1>

        <!-- This displays an existing ad on the top host page -->
        <div id="someOtherDiv" style="width:320px; height:50px;">
            <script>
                googletag.cmd.push(()=> { googletag.display('someOtherDiv'); });
            </script>
        </div>

        <!-- 
            Normally if the host page were displaying the 'myDiv' ad here, we'd uncomment the block below,
            but we want to run this functionality from inside the iframe's mycode.html below.
        -->
        <!-- 
        <div id="myDivId" style="width:320px; height:50px;">
            <script>
                googletag.cmd.push(()=> { googletag.display('myDiv'); });
            </script>
        </div>
        -->

        <hr>

        <!-- My code arrives in here and loads mycode.html -->
        <iframe style="width:320px; height:50px;" src="//example.org/mycode.html"></iframe>

    </body>
</html>
<!-- mycode.html Source -->
<html>
    <body>
        <div id="myDiv">
            <script>
                var myGoogletag = top.googletag;

                // Calling top.googletag.pubads().getSlots() here seems to see the slot set up with the 'myDiv' reference.
                // ...But the line below does not do anything.
                myGoogletag.cmd.push(function() { myGoogletag.display("myDiv"); });
            </script>
        </div>
    </body>
</html>

我希望看到广告显示在 iframe 中,但没有任何反应,也没有错误。

4

0 回答 0