检测 adblock 和 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4840368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:04:10  来源:igfitidea点击:

Detect adblock and javascript

javascriptadblock

提问by Dheeraj

I wish to detect adblocking software on my website and ask users to disable adblock by redirecting. The only way I found was using Javascript.

我希望在我的网站上检测广告拦截软件,并要求用户通过重定向来禁用广告拦截。我发现的唯一方法是使用 Javascript。

  1. Is there any other way of detection ?

  2. If not, how do I detect if Javascript is disabled and redirect them to a certain page ?

  1. 有没有其他检测方法?

  2. 如果没有,我如何检测 Javascript 是否被禁用并将它们重定向到某个页面?

采纳答案by Gabriel Hurley

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.

您实际上无法“检测”javascript 是否被禁用。由于 javascript 是客户端功能,因此服务器无法检测到它,而“检测”客户端的事情是使用 javascript 完成的。你看到了捕获 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.

可用的是<noscript>标签,它只有在 javascript 关闭时才由浏览器呈现。如果 javascript 被禁用,这是向用户显示消息的标准机制。使用 noscript 和巧妙的 CSS,您可以强制用户启用 javascript 或按照您提供的重定向链接使用您的网站。

There is no way to automatically redirect onlyusers 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.

无法自动重定向禁用 javascript 的用户。您可以使用 javascript 有选择地重定向用户,也可以根据服务器端条件(HTTP 标头等)重定向用户。但是你不能抓住那个中间群体。

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.

至于检测广告拦截,这将因浏览器和广告拦截方法而异。它没有一致的标志,但您可以执行一些操作,例如通过 JavaScript 检查您的广告服务器的可用性,或检查您的广告内容是否已加载到页面上。

回答by Beau

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:

要检测用户是否阻止广告,您所要做的就是在广告 javascript 中找到一个函数并尝试对其进行测试。他们使用什么方法来屏蔽广告并不重要。以下是 Google Adsense 广告的外观:

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

此方法概述如下: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:

要重定向所有禁用 javascript 的用户,只需将此代码放在 HTML 的头部:

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

回答by vsync

I quote from this post about the subject:

我引用了这篇关于这个主题的帖子:

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

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

HTML

HTML

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

JS:

JS:

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

回答by Chaoley

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

我无法获得@Beau 的解决方案来检查“window.google_render_ad”,但在检查“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'.

也许自原始答案发布以来 Adsense 代码已更改,我在从 Adsense 下载的 JS 中找到了“google_jobrunner”,但没有找到“google_render_ad”。