Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

How to detect if AdSense has loaded an ad in JavaScript?

I'm trying to integrate AdSense into our Svelte/Sapper project using the code snippets from their docs:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>

<ins class="adsbygoogle"
  style="display: inline-block;"
  data-ad-client="ca-pub-**************"
  data-ad-slot="**********"
  data-ad-format="horizontal"
>
</ins>

<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

The ads do show up sometimes but at other times there is just a big blank slot left on my page.

I thought that I'd be able to check whether an ad's being displayed by looking for it in AdSense's iframe programmatically with JavaScript; however, I cannot do this because the iframe has a src pointing to Google's domain and not ours, thus throwing a DOM exception.

Does anyone know how I can check if AdSense successfully loaded an ad or not so that I could decide whether to show something else in the blank spot that was supposed to be taken by the ad?

question from:https://stackoverflow.com/questions/65937507/how-to-detect-if-adsense-has-loaded-an-ad-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...