【发布时间】:2017-01-10 08:16:01
【问题描述】:
我使用这个简单的代码来检测 adblock 是否处于活动状态
<script>
(function(){
var test = document.createElement('div');
test.innerHTML = ' ';
test.className = 'adsbox';
document.body.appendChild(test);
window.setTimeout(function() {
if (test.offsetHeight === 0) {
alert("active");
} else {
alert("not active");
}
test.remove();
}, 100);
})();
到目前为止,这工作正常。
我需要一个 jquery 弹出窗口,而不是显示警报消息。所以我把上面的代码改成:
<script>
(function(){
var test = document.createElement('div');
test.innerHTML = ' ';
test.className = 'adsbox';
document.body.appendChild(test);
window.setTimeout(function() {
if (test.offsetHeight === 0) {
showmessage();
}
test.remove();
}, 100);
})();
</script>
没有这个功能
showmessage();
完美运行。
当 showmessage();在这个函数里面,弹出窗口没有出现。取而代之的是 showmessage();内容显示在没有 CSS 的网站上,而不是作为弹出窗口。我认为这与超时功能有关。但如果没有这个超时,检测在 Firefox 中不起作用。
来自 showmessage() 的内容;
function showmessage(){
document.write("<div id=\"boxes\">");
document.write(" <div id=\"dialog\" class=\"window\">");
document.write(" <div style=\"display: none; opacity: 0.8;\" id=\"mask\">
<\/div>");
document.write("<\/div></div>");
setTimeout(
function() {
var id = '#dialog';
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
//transition effect
$('#mask').fadeIn(500);
$('#mask').fadeTo("slow",0.9);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
//$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);
//transition effect
$(id).fadeIn(1000);
//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();
$('#mask').hide();
$('.window').hide();
});
//if mask is clicked
/*$('#mask').click(function () {
$(this).hide();
$('.window').hide();
}); */
}, 200);
}
非常感谢
【问题讨论】:
-
您不想使用
document.write,如果在页面加载后调用它会覆盖所有页面的内容。基本上,只需设置一个div默认隐藏并在用户使用任何广告拦截器时显示它 -
只要弹出窗口出现就没有关系。 showmessage() 工作正常。谢谢
-
showmessage() works fine不,它没有......它不像你期望的那样工作。在这里再次查看原因:stackoverflow.com/questions/19941866/… -
好的,谢谢,我明白了。
标签: javascript jquery