jQuery append Google Adsense to div

后端 未结 10 491
醉梦人生
醉梦人生 2020-11-29 06:05

I\'m having issues with google adsense and it loading before my jQuery and killing my codes, so I thought I\'d try to append the Google Adsense javascript to the appropriate

相关标签:
10条回答
  • 2020-11-29 06:39

    You can't include external scripts that way.

    To include javascript after the page has loaded, you should use jQuery's jQuery.getScript() function, but I don't know if that would work for Google Adsense.

    A little more info can be found here:

    http://geek.littleredstring.com/17-load-adsense-last-jquery

    0 讨论(0)
  • 2020-11-29 06:39

    Only this code working

    var i = 0;
    obj = $(".materialContent p");
    size = obj.size();
    for (i=0; i<size; i++)
    {
        cur = obj[i];
        if (i == 2)
        {
            $("#mainTopAdvDiv").appendTo(cur); 
        }
    }
    

    Do not remove, move full div.

    0 讨论(0)
  • 2020-11-29 06:44

    The way I do this is by having a placeholder on the spot I want the ad to appear.

    <html>
       <body>
          <div id="googleadgoeshere"></div>
       </body>
    </html>
    

    Then place the google-code in a container at the end of the page.

    <div id="adsense" style="display:none;">all the google javascript goes here</div>
    

    I then use jQuery to move the iframe the adsense code creates once the page is done loading.

    $(window).load(function(){
        $("#adsense").find("iframe").appendTo("#googleadgoeshere"); 
        $("#adsense").remove();
    });
    

    If you just try to move the #adsense div you will end up with a blank page. If you try to do this most any other way, you will end up with a blank page. Google had built in active ways to check that the code is not moved. If it is, your page will be blank. Why google has done this is beyond me, but I have found this workaround to work for me...

    0 讨论(0)
  • 2020-11-29 06:46

    Add a hidden DIV with adsense code in your page somewhere:

    <div id='adsense' style="display:none">
        <script type="text/javascript"><!--
            google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
            google_ad_slot = "xxxxxxxxxx";
            google_ad_width = 300;
            google_ad_height = 250;
            //-->
        </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
    </div>
    

    Using javascript create dynamic DIV for your ad to load:

    $("body").append("<div id='adslot' ></div>");
    

    Jquery code to insert the Ad:

    var ad = $("#adsense").html();
    $("#adslot").html(ad);
    

    This works for me.

    0 讨论(0)
提交回复
热议问题