Detect adblock and javascript [closed]

荒凉一梦 提交于 2019-11-30 07:01:48

You cannot actually "detect" if javascript is disabled. Since javascript is a client-side feature, the server cannot detect it, and "detecting"things client-side is done with javascript. You see the catch 22.

What is available is the <noscript> tag, which is only rendered by the browser if javascript is turned off. This is the standard mechanism for displaying a message to a user if javascript is disabled. Using noscript and clever CSS you can make it imperative that users either enable javascript or follow a redirect link you present to use your site.

There is no way to automatically redirect only users that have javascript disabled. You can redirect users selectively by using javascript, or you can redirect people based on server-side criteria (HTTP headers, etc.). But you can't catch that middle group.

As for detecting adblocking, this is going to vary by browser and adblocking method. There isn't a consistent flag for it, but you can do things like checking for the availability of your ad server via javascript, or checking if your ad content is loaded on the page.

To detect if the user is blocking ads, all you have to do is find a function in the ad javascript and try testing for it. It doesn't matter what method they're using to block the ad. Here's what it looks like for Google Adsense ads:

if(typeof(window.google_render_ad)=="undefined") 
{ 
    //They're blocking ads, do something else.
}

This method is outlined here: http://www.metamorphosite.com/detect-web-popup-blocker-software-adblock-spam

To redirect all users with javascript disabled, simply put this code in the head of your HTML:

<noscript>
    <meta http-equiv="refresh" content="5;url=http://newsite.com/">
</noscript>
vsync

I quote from this post about the subject:

http://w3guy.com/detecting-adblock/

HTML

<div class="myTestAd">
    <!-- Adsense Ad code goes here -->
</div>

JS:

if ($('.myTestAd').height() == 0) {
    // stuff to do if adBlock is active
} 

I couldn't get @Beau's solution to work checking for 'window.google_render_ad' but it did work when checking 'window.google_jobrunner'.

Maybe the Adsense code has changed since the original answer was posted, I found 'google_jobrunner' in the JS downloaded from Adsense but not 'google_render_ad'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!