在adblock处于有效状态时显示弹出窗口

时间:2016-09-02 09:10:21

标签: javascript jquery

我使用这个简单的代码来检测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);
}

非常感谢

1 个答案:

答案 0 :(得分:2)

对pre使用prepend方法,然后将弹出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);
    }
相关问题