Frage

If user has some kind of ad blocker installed, ad blocker will of course remove all ads from my website, and it leaves empty spaces where the ads used to be. I would like to use that empty space by putting some other content in it, like links to most important pages of my website, to do that I need to detect if AdSense javascript is loaded.

Methods that I have tried so far:

if (!document.getElementById("google_ads_frame1"))
{
}

and:

if (typeof(window.google_render_ad) == "undefined")
{
}

Both of those seem to fail in certain situation, for example if browser downloads AdSense javascript files a bit slower, it will execute above mentioned code before AdSense code is loaded and I end up hiding ads for users that don't even have ads blocked.

Do you have any suggestions on how could I make sure that my code is run after AdSense? Or some other way of detecting that AdSense scripts are not loaded?

War es hilfreich?

Lösung

Run this code on the window.onload event. window.onload event is fired when the page has completed loading.

window.onload = function() {
  // your checks
}

If you're using jQuery, use

$(window).load(function() {
  // your checks
});

Andere Tipps

If using the new AdSense code, you can do an easy check, without resorting to content or css checks.

Place your ads as normal in your markup:

<ins class="adsbygoogle" style="display: block;"
   data-ad-client="ca-pub-######"
   data-ad-slot="#######"
   data-ad-format="auto"></ins>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

Then you call the adsense code at the bottom of your page (note do not use the "async" flag when calling the adsbygoogle.js script):

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

Then add this little snippet of code below that:

<script>
if (!adsbygoogle.loaded) {
   // do something to alert the user
}
</script>

AdSense always creates/sets the flag adsbygoogle.loaded to true when the ads are loaded. You could place the check in a setTimeout function to delay the check by a few seconds.

This ultra-lightweight method seems to be infallible and is agnostic to all ad networks.

Place a file call ads.js in your root folder and add this line inside it:

var can_run_ads = true;

Adblock won't allow a file named ads.js to be loaded. So, just place this code before </body>

<script src="/ads.js"></script>
<script>
if(window.can_run_ads === undefined) {
    alert('Please Disable Adblock, ****** ******!');
}
</script>';

I strongly recommend to redirect the user to a page that has instructions on how to remove Adblock.

I suggest you use FuckAdBlock to detect if AdBlock is activated. If this is the case, you are free to add as many html in your page you want.

See the documentation for examples: https://github.com/sitexw/FuckAdBlock

After some more tests I realized that issue was not about timing as I previously thought. Problem was that AdSense started to use some new code, this code doesn't have "google_render_ad" function and it doesn't create iframe with ID "google_ads_frame1" (new ID is "aswift_0"). Considering that AdSense now uses at least two different IDs for iframes I made new detection code that checks just for presence of iframe element regardless of its ID:

if (document.getElementsByTagName("iframe").length === 0)
{
    // ad is blocked
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top