jQuery append Google Adsense to div

后端 未结 10 490
醉梦人生
醉梦人生 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:23

    After trying and failing with the codes on here, I ended up taking the jQuery object that I was using and putting it in a new html page. Once I did that, I just used an iframe to place it on the page with the adsense.

    Now, adsense and jQuery run at the same time with no problems.

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

    In the link to the page that was being loaded via ajax, I set data-ajax="false" and then on the page that was loaded I ask the user to use the back button.

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

    I had this exact same problem. I eventually solved it by using jQuery to load an with a src pointing to an html file that contains the Google AdSense javascript. This is rather inelegant, since the Google AdSense code creates an <iframe>. And, I happen to be working with a Facebook application, so in my case, I've got an <iframe> (the Google Ad) in an <iframe> (the one I used to get around the Firefox error) in an <iframe> (my Facebook app). But, it works.

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

    I use this variant. All good!

    $(document).ready(function() {
    $("div.youtube_ad").html("<iframe src='/adsense.html' width='350' height='290' scrolling='no'></iframe>")
    });
    

    In adsense.html put you AdSense code 'AsIs'.

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

    This is how I would do it. Simple and short.

    <script>
    $(window).load(function(){
        $('#adsense').fadeIn(0);
    });
    </script>
    
    <div id="adsense" style="display:none;">
       [YOUR ADSENSE SCRIPT HERE]
    </div>
    
    0 讨论(0)
  • 2020-11-29 06:32

    Danny's answer explained the basic solution for this after I unsuccessfully experimented extensively on my own, so thanks! However, I needed a more elaborate solution as I had an unknown number of ads that I wanted to replace on a given page, so here's what I did:

    The basic html (php) outline, ala Danny's format -- note my incrementing of the ad count, based on various factors of rows from a db query, i.e. such that it is impossible to know the count beforehand:

    <html>
       <body>
    <? while ($r = mysql_fetch_assoc($rs)) { if (true) { ?>
          <div class="adslide"><?=$ads++?></div>
    <? } } ?>
       </body>
    </html>
    

    I separated out the css for the adsense div I'll create in a moment as I have one for each adslide div created above:

    <style> .adsense { display: none; } </style>
    

    Here, placed in the bottom of the page, I get the actual ads from google into the html, the count being determined by how many slots I have for them from above:

    <?php for ($i = 0; $i < $ads; $i++) { echo '<div class="adsense">'.$adscript.'</div>'; } ?>
    

    And finally I cycle through all the adsense ads written into the html and fill them in, one by one, into the adslide slots that were created in the html, making sure each ad and slot are only used/filled once by removing them or their class after I'm done with them:

    <script>
    // http://stackoverflow.com/questions/1142861/jquery-append-google-adsense-to-div
    $(function () { var b, a = $(".adsense").first();
     for (; a.length > 0; a = $(".adsense").first())
     { b = $(".adslide").first(); b.append(a.find("iframe"));
      a.remove(); b.removeClass("adslide"); } });
    </script>
    

    This is an extremely weird bug from google. I can only assume it's related to some protection google created to prevent people from hiding their ads (by positioning them offscreen or behind other html elements or something) to try and collect impression counts without actually displaying ads (i.e. so you could put a million of these in the html but the user would never see them, and you collect the cash until google finds out). However, the fact that this bug does not show up in IE and Safari but does in Firefox and Google's own Chrome... That's weird. They should definitely fix this on their side.

    For those who are working with the same software: I ran into this problem myself when implementing a jQuery Carousel (http://sorgalla.com/projects/jcarousel/) that had ads mingled with user submitted photos in the carousel.

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