检测用户是否启用了adblocker,重定向到另一个页面,但仅一次&;使用jQuery显示广告的备选方案
这个问题已经被问了好几次了,但从来没有像这样。我想要什么:检测用户是否启用了adblocker,重定向到另一个页面,但仅一次&;使用jQuery显示广告的备选方案,jquery,redirect,ads,adblock,Jquery,Redirect,Ads,Adblock,这个问题已经被问了好几次了,但从来没有像这样。我想要什么: 检查用户是否已启用adblocker 如果是,请将用户重新定向到登录页 在该登录页上,显示一个链接,将用户指向原始目的地 只要启用adblocker,用户将只被定向到该登录页一次(或每x天一次) 广告(隐藏的)必须替换为自定义HTML 最好使用jQuery(客户端,因为据我所知,不可能检测到adblocker服务器端) 注:我只关心谷歌广告。测试信息 2013年11月21日的答复。解决方案可能不适用于所有年龄段 仅在Chrome
- 检查用户是否已启用adblocker
- 如果是,请将用户重新定向到登录页
- 在该登录页上,显示一个链接,将用户指向原始目的地
- 只要启用adblocker,用户将只被定向到该登录页一次(或每x天一次)
- 广告(隐藏的)必须替换为自定义HTML
- 2013年11月21日的答复。解决方案可能不适用于所有年龄段
- 仅在Chrome 31和Firefox 25上测试,在Windows 8 64位计算机上测试
- 提高了本文档的可读性
- 在您的站点上运行的.js文件(此后称为
)scripts.js
- 广告的div包装。不要使用太容易被检测到的与广告相关的类!尝试类似于
的方法。因此,您可以使用该类将每个广告单独包装在一个div中,例如class=“cont ad”
- 一个你解释为什么你需要在你的网站上做广告的页面。我称之为ads.html。为该页面的body标记指定一个易于识别但又唯一的类。(你可以用ID代替,但我发现类更容易。)我将在ads上给主体一个class
李>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/js/jquery.cookie.js"></script>
<script src="/js/scripts.js"></script>
注意最后一个条件也可以写成。是(“:hidden”)
。我以为看不见更清楚
正如我之前所说,这些条件检查广告是否被“屏蔽”。当这返回真的,我们可以做我们想要的
更改内容并添加另一个If子句
首先,我们想展示一些隐藏广告的替代品。我们将更改广告包装的HTML。这将完全删除广告的代码,但这并不重要,因为它已被阻止
$(".cont-ad").html('<span class="adblock-message">Please. Do not take my ads away! <a href="http://www.yoursite.com/ads.html" target="_self" title="Ads">Read more.</a></span>');
我们只想替换广告,并且只在广告被阻止时重定向用户,因此我们将上述所有内容放在第一个if子句中:
if ($("ins.adsbygoogle").is(':empty') ||
$("ins.adsbygoogle").height() === 0 ||
!$("ins.adsbygoogle").is(":visible")) {
// Change the HTML content of the ad
$(".cont-ad").html('<span class="adblock-message">Please. Do not take my ads away! <a href="http://www.yoursite.com/ads" target="_self" title="Ads">Read more.</a></span>');
if ($.cookie('ads_checked') == null) {
// Set cookie with value of current page
$.cookie('locational_cookie', window.location, {
expires: 7, // Not really necessary
path: '/'
});
// Redirect
window.location = "http://www.yoursite.com/ads.html";
}
}
但是我们在这个函数中需要什么呢?首先,我们需要将“back”链接的href设置为原始位置。我们将原始目的地保存在cookie中,因此这非常有用!在那之后,我们想要删除那个cookie(locational\u cookie
)-我们不再需要它了。不过,我们还需要一块饼干。(饿了吗?)
回想一下前面构建的代码块:在嵌入的if子句中,我们检查是否存在名为ads\u checked
的cookie。如果该cookie不存在,我们希望将用户发送到登录页,但我们只希望将其发送一次。所以我们要做的是:当用户在这个登录页上时,设置一个cookie——如果cookie存在,用户就不能再次重定向。现在我们也创建了cookie
请注意,我们必须将反向链接附加到ads.HTML
页面上的某个HTML元素。在这里,它被称为#wrapper
if ($("body").hasClass("on-ads") && $.cookie("locational_cookie") != null) {
// Append back link
$("#wrapper").append('<p>Get to your <em><a href="' + $.cookie("locational_cookie") + '" target="_self">original destination</a></em>.</p>');
// Remove locational cookie
$.removeCookie('locational_cookie', {path: '/'});
// Set ads_checked cookie
$.cookie('ads_checked', 'true', {
expires: 7, // so the user will be redirected to the landing page again after 7 days
path: '/'
});
}
if($(“body”).hasClass(“在广告上”)和&$.cookie(“locational_cookie”)!=null){
//追加回链接
$(“#包装器”).append(“Get to your.”);
//删除位置cookie
$.removeCookie('locational_cookie',{path:'/'});
//将广告设置为选中的cookie
$.cookie('ads_checked'、'true'{
expires:7,//因此用户将在7天后再次重定向到登录页
路径:'/'
});
}
结果
基本上就是这样!把所有的东西放在一起,我们得到了这个,在一个很好的文档准备功能中。这是答案,可能更清楚一点
$(document).ready(function () {
if ($("body").hasClass("on-ads") && $.cookie("locational_cookie") != null) {
// Append back link
$("#wrapper").append('<p>Get to your <em><a href="' + $.cookie("locational_cookie") + '" target="_self">original destination</a></em>.</p>');
// Remove locational cookie
$.removeCookie('locational_cookie', {
path: '/'
});
// Set ads_checked cookie
$.cookie('ads_checked', 'true', {
expires: 7, // so the user will be redirected to the landing page again after 7 days
path: '/'
});
}
if ($("ins.adsbygoogle").is(':empty') || $("ins.adsbygoogle").height() === 0 || !$("ins.adsbygoogle").is(":visible")) {
// Change the HTML content of the ad
$(".cont-ad").html('<span class="adblock-message">Please. Do not take my ads away! <a href="http://www.yoursite.com/ads" target="_self" title="Ads">Read more.</a></span>');
if ($.cookie('ads_checked') == null) {
// Set cookie with value of current page
$.cookie('locational_cookie', window.location, {
expires: 7, // Not really necessary
path: '/'
});
// Redirect
window.location = "http://www.yoursite.com/ads";
}
}
});
$(文档).ready(函数(){
if($(“body”).hasClass(“在广告上”)和&$.cookie(“locational_cookie”)!=null){
//追加回链接
$(“#包装器”).append(“Get to your.”);
//删除位置cookie
$.removeCookie(‘位置_cookie’{
路径:'/'
});
//将广告设置为选中的cookie
$.cookie('ads_checked'、'true'{
expires:7,//因此用户将在7天后再次重定向到登录页
路径:'/'
});
}
如果($(“ins.adsbygoogle”).is(':empty')|$(“ins.adsbygoogle”).height()==0 | |!$(“ins.adsbygoogle”).is(“:可见”)){
//更改广告的HTML内容
$(“.contad”).html('请不要拿走我的广告!');
if($.cookie('ads_checked')==null){
//使用当前页面的值设置cookie
$.cookie('locational_cookie',window.location{
expires:7,//实际上不需要
路径:'/'
});
//重定向
window.location=”http://www.yoursite.com/ads";
}
}
});
你刚才问了一个问题并立即回答了吗?@tymeJV我问了。我想了好几天这个问题,本来想问的,但后来我想出来了。我想其他人可能也有同样的问题,所以我同时发布了问题和答案。我花了一段时间。非常彻底,非常好!
if ($("body").hasClass("on-ads") && $.cookie('locational_cookie') != null) {
if ($("body").hasClass("on-ads") && $.cookie("locational_cookie") != null) {
// Append back link
$("#wrapper").append('<p>Get to your <em><a href="' + $.cookie("locational_cookie") + '" target="_self">original destination</a></em>.</p>');
// Remove locational cookie
$.removeCookie('locational_cookie', {path: '/'});
// Set ads_checked cookie
$.cookie('ads_checked', 'true', {
expires: 7, // so the user will be redirected to the landing page again after 7 days
path: '/'
});
}
$(document).ready(function () {
if ($("body").hasClass("on-ads") && $.cookie("locational_cookie") != null) {
// Append back link
$("#wrapper").append('<p>Get to your <em><a href="' + $.cookie("locational_cookie") + '" target="_self">original destination</a></em>.</p>');
// Remove locational cookie
$.removeCookie('locational_cookie', {
path: '/'
});
// Set ads_checked cookie
$.cookie('ads_checked', 'true', {
expires: 7, // so the user will be redirected to the landing page again after 7 days
path: '/'
});
}
if ($("ins.adsbygoogle").is(':empty') || $("ins.adsbygoogle").height() === 0 || !$("ins.adsbygoogle").is(":visible")) {
// Change the HTML content of the ad
$(".cont-ad").html('<span class="adblock-message">Please. Do not take my ads away! <a href="http://www.yoursite.com/ads" target="_self" title="Ads">Read more.</a></span>');
if ($.cookie('ads_checked') == null) {
// Set cookie with value of current page
$.cookie('locational_cookie', window.location, {
expires: 7, // Not really necessary
path: '/'
});
// Redirect
window.location = "http://www.yoursite.com/ads";
}
}
});