开发者_StackOverflow中文版
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this questionI wish to detect adblocking software on my website and ask users to disable adblock by redirecting. The only way I found was using Javascript.
Is there any other way of detection ?
If not, how do I detect if Javascript is disabled and redirect them to a certain 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>
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.
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'.
精彩评论