0

My goal is NOT dispaly adsense when browser is cordova

So I have these 2 codes:

<script>
var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
if ( !app ) {
    // DISPLAY adsense here
}  
</script>

and adsense block:

<div  style="margin-bottom:25px;height:180px;"> 
  <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 
  <!-- responsive-adaptable --> 
  <ins class="adsbygoogle"
             style="display:block"
             data-ad-client="ca-pub-xxxxx"
             data-ad-slot="6768980312"
             data-ad-format="auto"></ins> 
  <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script> 
</div>

I just don't know how to glue them together !

yarek
  • 11,278
  • 30
  • 120
  • 219

1 Answers1

1

This should work:

<script>
var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
if ( !app ) {
    // DISPLAY adsense here
    var adSenseSript = document.createElement('script');
    adSenseSript.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
    adSenseSript.async = true;
    document.head.appendChild(adSenseSript);

   // Also hiding the ad boxes by default and displaying them later if good idea

}  
</script>

I am sure this is no way violating the AdSense policies as far as it is concerned. It simply creates a new script element and loads the main script file for AdSense to work on the specified condition.

As per the implementation guide, it isn't necessary to include <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> with each ad-space, so you can simply omit the script tag and load just one instance of the script.

From this page: https://support.google.com/adsense/answer/3221666?hl=en&ref_topic=1307438

If I have multiple ad units on a page, do I have to include for each ad unit? No, this is not necessary, adsbygoogle.js can be loaded once.

Deepak Kamat
  • 1,880
  • 4
  • 23
  • 38