我试图在我的网站上使用css3制作一个Dialog,如果用户正在使用adblock,它会弹出。该对话框基本上要求用户在我的网站上启用广告,但我不想在每次访问我的网站时弹出对话框来惹恼用户,所以我想给用户一个"不要再次告诉我"选项,但我不知道如何实现这一点。
这是对话框的图像
这是检测adblock用户并打开对话框
的java脚本代码<script> window.setTimeout(function(){
if(adsbygoogle instanceof Array) {
// adsbygoogle.js did not execute; probably blocked by an ad blocker
window.location = '#PleaseUnblock'
} else {
// adsbygoogle.js executed
}
}, 2000);
</script>
这是对话框的HTML代码
<div id="PleaseUnblock" class="modalDialog">
<div style="margin-top: 70px;padding: 5px 12px 13px 15px;width: 380px;">
<a href="#" title="Close" class="close">X</a>
<div style="padding: 0; margin: 0;">
<h1 class="ad-di-title">You Are Using AdBlock :(</h1>
</div>
<div style="margin-left: auto;margin-right: auto;width: 350px;height: 332px;background: url(/assets/please.jpg);background-size: 350px;background-repeat: no-repeat"></div>
<p class="ad-di-para" style="margin-bottom: 3px;">All content made on WEBSITENAME is free. As you use Adblock, we wont get any revenue from the content we make. We promise that we will not show annoying ads and we only ask you to enable ads in our site.</p>
<a style="margin-left: 300px" href="#" class="kbtn">Ok</a>
</div>
</div>
答案 0 :(得分:2)
使用Cookie的一个相当简单的解决方案:
function askLater() {
var d = new Date();
d.setTime(d.getTime() + (86400000)); //set the time to 24 hours later
var expires = "expires="+d.toUTCString();
document.cookie = "dontAsk=true; " + expires;
}
如果用户按下&#34;请不要再次问我&#34;请拨打功能askLater()
按钮。
然后,在代码中测试cookie dontAsk,例如:
if(adsbygoogle instanceof Array && document.cookie.indexOf("dontAsk") == -1) {
// indexOf is -1 when string not found, so the cookie is unset
// adsbygoogle.js did not execute; probably blocked by an ad blocker
window.location = '#PleaseUnblock'
} else { ... }
如果您想再也不要问,请改用:
function neverAsk() {
document.cookie="dontAsk=true";
就这么简单。