2

Insert Google Block Ads with Javascript

 2 years ago
source link: https://stackoverflow.com/questions/71906081/insert-google-block-ads-with-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Insert Google Block Ads with Javascript

I have a site with many pages and I want to start with Google Ads on it... so I want to insert ads to all the pages, but I want to find a good way... I don't want to add the code manually in all the files... so I tried to build a function to add the google's adsense block after some paragraphs using javascript:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXX" crossorigin="anonymous"></script>

var display_block = '<ins class="adsbygoogle"' +
        'style="display:block"' +
        'data-ad-client="ca-pub-XXXXX"' +
        'data-ad-slot="xxxxxxxxxx"' +
        'data-ad-format="auto"' +
        'data-full-width-responsive="true"></ins>' +
        '<script>' +
        '(adsbygoogle = window.adsbygoogle || []).push({});' +
        '</script>';

    var afterparagraph1 = document.getElementById('article').getElementsByTagName('p')[0]
    afterparagraph1.insertAdjacentHTML('afterend', display_block );

    var afterparagraph3 = document.getElementById('article').getElementsByTagName('p')[3]
    afterparagraph3.insertAdjacentHTML('afterend', display_block );

Well the code is added (I can see when inspect with console) but the ads doesn´t appear... but if I insert the code manually inside each html file it appears... :(

Do you know what I am doing wrong?

Thanks for your time :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK