【问题标题】:Show popup when adblock is active当 adblock 处于活动状态时显示弹出窗口
【发布时间】:2017-01-10 08:16:01
【问题描述】:

我使用这个简单的代码来检测 adblock 是否处于活动状态

<script>
(function(){
 var test = document.createElement('div');
 test.innerHTML = '&nbsp;';
 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 = '&nbsp;';
 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


【解决方案1】:

使用 prepend 方法到 body 中,然后将弹出的 HTML 字符串设置到方法中。 :

function showmessage(){
        var jQueryPopupHTML = "<div id='boxes'><div id='dialog' class='window'> <div style='display: none;  opacity: 0.8;' id='mask'></div></div></div>"
        $( "body" ).prepend(jQueryPopupHTML);

    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 无法向页面添加更多内容,而是完全覆盖现有内容。相反,您应该使用document.createElementnode.appendChild 之类的方法向页面添加新内容(或jQuery 的append/prepend)。这也使您可以控制将内容添加到 DOM 树的位置
  • tx 用于解释。我明白。你知道为什么淡入效果不再起作用了吗?
  • 好的,找到了。 $(jQueryPopupHTML).hide().prependTo("body").fadeIn("slow");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2022-09-28
  • 1970-01-01
相关资源
最近更新 更多