【问题标题】:using js/jquery to populate google adsense使用 js/jquery 填充 google adsense
【发布时间】:2014-01-16 07:37:48
【问题描述】:

我正在尝试为我的网站合并我自己的广告模块,如果它们没有要显示的广告,则默认为 google adesense 广告

我现在有

if(this.ad_found)
//run code to display ad
else {
    google_ad_client = "ca-pub-";
    /* Name */
    google_ad_slot = "-----";
    google_ad_width = ---;
    google_ad_height = ---;

    var head = document.getElementById('id_from_display_page')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '//pagead2.googlesyndication.com/pagead/show_ads.js';
    $("#id_from_display_page").html(script);
}

如您所知,我不知道如何填充 id 'id_from_display_page' 以显示 google adsense 内容

显示页面:

<div id="id_from_display_page"></div>

我想用 google adsense 填充这个 div

【问题讨论】:

    标签: javascript jquery adsense


    【解决方案1】:

    编辑:

    这一次,我们将把所有这些封装在一个自执行函数中。此外,我们将制作新元素并将它们粘贴在最后的body 中。这是备忘单:

    Javascript:

    (function (window, document) {
        if (ad_found) {
            //run code to display ad
        } else {
            google_ad_client = "ca-pub-";
            /* Name */
            google_ad_slot = "-----";
            google_ad_width = "---";
            google_ad_height = "---";
    
            var head = document.createElement('div');
            head.id = "id_from_display_page";
            var script = '<script src="//pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"><\/script>';
            head.innerHTML = script;
            document.body.appendChild(head);
        }
    })(this, this.document, undefined);
    

    不幸的是,如果没有设置这些变量,我无法实际测试它。但我决定将 &lt;script&gt; 放在 new &lt;div&gt; 元素中,该元素必须由 DOM 解析,并应导致代码按预期执行。

    随时通知我。

    除了缺少的变量,我在下面制作的小提琴看起来一切都很顺利:)

    Demooooooooo

    【讨论】:

    • 编辑:它正在运行脚本,我用另一个 js 文件进行了测试,它会发出 hi 警报,并且确实如此。 google adsense 的东西没有显示
    • hahahaha 绝对可以肯定你有一组我刚刚为你重做的备份代码。无论哪种方式,您都不会出错。
    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 2012-07-13
    • 2014-08-22
    相关资源
    最近更新 更多