Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Angelic Sanoy
Angelic Sanoy
7,775 Points

Embeeding adsense using JQuery

Hi, I tried to put this code on my HubsPot blog.. What I basically want is to pop-the ad unit in every 5th and 10th pagraph, I'm having issue of the the ads.. I tried to use string and iframe and it works but it's not cooperating in Adsense code. Does jQuery doesn't support script embed? Here's the sample blog https://blog.hollywoodbranded.com/the-power-of-brand-partnerships-a-stranger-things-case-study . Do I need to initialize the script? https://stackoverflow.com/questions/610995/cant-append-script-element

This code works:

$(' p:eq(5), p:eq(10)').after($('<div/>', {  
           html: ' <iframe width="560" height="315" src="https://www.youtube.com/embed/wlpTEghVytI" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> '
          }));

This code works:

$(' p:eq(5), p:eq(10)').after($('<div/>', {  
           html: ' This is a test Adsense '
          }));

This code doesn't work

$(' p:eq(5), p:eq(10)').after($('<div/>', {  
           html: ' <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-7533231660623439"
     data-ad-slot="8541141677"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script> '
          }));

1 Answer

Angelic Sanoy
Angelic Sanoy
7,775 Points

I used old version of Google Adsense to run the code

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