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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…